public async Task <FileStreamResult> RenderViewAsPdf(string viewName, object model, string fileName) { var htmlString = await RenderViewAsHtml(viewName, model); MemoryStream inMemoryStream = new MemoryStream(); SelectPdf.HtmlToPdf converter = new SelectPdf.HtmlToPdf(); SelectPdf.PdfDocument doc = converter.ConvertHtmlString(htmlString); doc.Save(inMemoryStream); doc.Close(); inMemoryStream.Seek(0, SeekOrigin.Begin); var retStream = new FileStreamResult(inMemoryStream, new MediaTypeHeaderValue("application/pdf")); if (!string.IsNullOrEmpty(fileName)) { retStream.FileDownloadName = fileName; } return(retStream); }
private void ConvertToPDF(object state) { var p = state as object[]; var html = p[0].ToString(); var outputFile = p[1].ToString(); var converter = new HtmlToPdf { Options = { PdfPageSize = PdfPageSize.A4, PdfPageOrientation = PdfPageOrientation.Portrait, WebPageWidth = 210, WebPageHeight = 297, CssMediaType = HtmlToPdfCssMediaType.Screen, DrawBackground = true } }; //Generate PDF file var doc = converter.ConvertHtmlString(html); if (File.Exists(outputFile)) { File.Delete(outputFile); } doc.Save(outputFile); doc.Close(); if (ConfigInfo.SaveHtml) { File.WriteAllText(outputFile.Replace(".pdf", ".html"), html, Encoding.UTF8); } BeginInvoke((Action)(() => { barProgress.PerformStep(); lblStatus.Text = $@"Archivo PDF {barProgress.Value}/{barProgress.Maximum}"; })); }
/// <summary> /// Generate pdf from html string with custom settings. /// </summary> /// <param name="settings">Pdf convertation settings</param> /// <param name="htmlString"></param> /// <returns>Pdf data is an memory stream.</returns> public Stream ConvertToPdfAsStream(HtmlToPdfSettings settings, string htmlString) { if (settings == null) { throw new ArgumentNullException("settings", "Pdf settings is null"); } if (string.IsNullOrEmpty(htmlString)) { throw new ArgumentNullException("htmlString", "Source html is empty"); } ApplyUserSettings(settings); var pdfContainer = _internalConverter.ConvertHtmlString(htmlString); var pdfStream = new MemoryStream(); pdfContainer.Save(pdfStream); return(pdfStream); }
public static byte[] GetSelectPdf(string htmlString, PdfPageSize pdfPageSize, string Titulo = "", string Autor = "", string Asunto = "", string PalabraClave = "") { byte[] PDFBytes = null; try { SelectPdf.HtmlToPdf htmlToPdf = new SelectPdf.HtmlToPdf(); htmlToPdf.Options.PdfPageSize = pdfPageSize; htmlToPdf.Options.PdfPageOrientation = PdfPageOrientation.Portrait; htmlToPdf.Options.AutoFitWidth = HtmlToPdfPageFitMode.ShrinkOnly; htmlToPdf.Options.WebPageWidth = 1000; htmlToPdf.Options.WebPageHeight = 0; htmlToPdf.Options.MarginLeft = 0; htmlToPdf.Options.MarginRight = 0; htmlToPdf.Options.MarginTop = 0; htmlToPdf.Options.MarginBottom = 0; htmlToPdf.Options.PdfDocumentInformation.Title = Titulo; htmlToPdf.Options.PdfDocumentInformation.Author = Autor; htmlToPdf.Options.PdfDocumentInformation.CreationDate = DateTime.Now; htmlToPdf.Options.PdfDocumentInformation.Subject = Asunto; htmlToPdf.Options.PdfDocumentInformation.Keywords = PalabraClave; //htmlToPdf.Options.JavaScriptEnabled = true; htmlToPdf.Options.RenderingEngine = RenderingEngine.WebKitRestricted; //htmlToPdf.Options.RenderPageOnTimeout = true; //htmlToPdf.Options.MinPageLoadTime = 2; //htmlToPdf.Options.CssMediaType = HtmlToPdfCssMediaType.Screen; SelectPdf.PdfDocument pdf = htmlToPdf.ConvertHtmlString(htmlString); MemoryStream ms = new MemoryStream(); pdf.Save(ms); pdf.Close(); PDFBytes = ms.ToArray(); } catch (Exception ex) { throw ex; } return(PDFBytes); }
private void GeneratePdf(string studentName, string studentId, DateTime date, string ccVnName, string ccEnName, string ccNumber, string folderStoragePath, string ccCode, string email, DoWorkEventArgs ev, SelectPdf.HtmlToPdf converter) { CertificateModel certificateModel = new CertificateModel(); string filePath = null; if (backgroundWorker1.CancellationPending) { ev.Cancel = true; } else { try { string finishedDate = date.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture); string[] parameters = { studentName, ccVnName, ccEnName, ccNumber, finishedDate, Application.ExecutablePath.Remove(Application.ExecutablePath.LastIndexOf("\\")).Replace("\\", "/") }; //string filename = "E:/Funix/Template certificate/certificate template 1.pdf"; filePath = $"{folderStoragePath.Replace("\\", "/")}/{ccNumber}-{ccCode}-{studentName}.pdf"; string html = string.Format(contentHtml, parameters); // define a rendering result object PdfDocument doc = converter.ConvertHtmlString(html); doc.Save(filePath); doc.Close(); //HtmlToImage htmlToImage = new HtmlToImage(); //Image img = htmlToImage.ConvertHtmlString(html); ////TODO: create new file first //FileStream stream = File.Open("D:/Image/test.png", FileMode.Create); //img.Save(stream, ImageFormat.Png); UploadFileToGoogleDrive(studentName, studentId, date, ccEnName, ccNumber, ccCode, email, filePath, ev); } catch (Exception e) { throw e; } } }
public string SendLOS(string Token) { var Member = _context.Users.FirstOrDefault(x => x.Token == Token); if (Member != null) { MemoryStream memoryStream = new MemoryStream(); StreamReader streamReader; streamReader = new StreamReader(Directory.GetCurrentDirectory().ToString() + "\\Controllers\\Documents\\Certificate.html"); if (streamReader != null) { string content = streamReader.ReadToEnd(); if (content != null) { content.ToString(); streamReader.Close(); string content1 = content.Replace("Name", Member.Username) .Replace("Type", "Letter Of Dedicated Status") .Replace("Date", "December 2019"); SelectPdf.HtmlToPdf converter = new SelectPdf.HtmlToPdf(); converter.Options.PdfPageSize = PdfPageSize.A4; converter.Options.PdfPageOrientation = PdfPageOrientation.Landscape; converter.Options.AutoFitHeight = HtmlToPdfPageFitMode.AutoFit; converter.Options.AutoFitWidth = HtmlToPdfPageFitMode.AutoFit; PdfDocument doc = new PdfDocument(); doc = converter.ConvertHtmlString(content1); if (doc != null) { doc.Save(memoryStream); byte[] bytes = memoryStream.ToArray(); memoryStream.Close(); if (bytes != null) { sendMail.SendEmail(Member.Email, "Letter Of Dedicated Status", new Attachment(new MemoryStream(bytes), "LOS.pdf")); doc.Close(); return("Document Sent"); } return("Document Not Sent"); } return("Document Not Sent"); } return("Document Not Sent"); } return("Document Not Sent"); } return("Document Not Sent"); }