public static void Html2Pdf(string htmlUrl, string filename, string footer = "") { #region 使用Select.PDf // instantiate the html to pdf converter HtmlToPdf converter = new HtmlToPdf(); // convert the url to pdf PdfDocument doc = converter.ConvertUrl(htmlUrl); if (!string.IsNullOrEmpty(footer)) { // page numbers can be added using a PdfTextSection object PdfTextSection text = new PdfTextSection(0, 10, footer, new System.Drawing.Font("Arial", 8)); text.HorizontalAlign = PdfTextHorizontalAlign.Center; converter.Footer.Add(text); } // save pdf document doc.Save(filename); // close pdf document doc.Close(); #endregion return; #region 使用wkhtml2pdf string exeFilePath = Directory.GetCurrentDirectory() + @"\wkhtmltopdf.exe"; //MoveFolderTo(fileName, Application.StartupPath + @"\PDFLIB\"); //生成ProcessStartInfo ProcessStartInfo pinfo = new ProcessStartInfo(exeFilePath); //设置参数 StringBuilder sb = new StringBuilder(); sb.Append("--footer-line "); sb.Append($"--footer-center \"{footer}\" "); sb.Append("\"" + htmlUrl + "\""); sb.Append(" \"" + filename + "\""); pinfo.Arguments = sb.ToString(); //隐藏窗口 pinfo.WindowStyle = ProcessWindowStyle.Hidden; //启动程序 Process p = Process.Start(pinfo); p.WaitForExit(); //DeleteFiles(Application.StartupPath + @"\PDFLIB\"); if (p.ExitCode == 0) { } else { throw new Exception("生成PDF文件失败:" + sb.ToString()); } #endregion }
public static void ConvertContractHtmlCodeToPdf(PdfModel model) { HtmlToPdf converter = new HtmlToPdf(); if (model.HeaderUrl != null) { // header settings converter.Options.DisplayHeader = true; converter.Header.DisplayOnFirstPage = true; converter.Header.DisplayOnOddPages = true; converter.Header.DisplayOnEvenPages = true; converter.Header.Height = 70; // add some html content to the header PdfHtmlSection headerHtml = new PdfHtmlSection(model.HeaderUrl); headerHtml.AutoFitHeight = HtmlToPdfPageFitMode.AutoFit; headerHtml.AutoFitWidth = HtmlToPdfPageFitMode.AutoFit; converter.Header.Add(headerHtml); } if (model.FooterUrl != null) { // footer settings converter.Options.DisplayFooter = true; converter.Footer.DisplayOnFirstPage = true; converter.Footer.DisplayOnOddPages = true; converter.Footer.DisplayOnEvenPages = true; converter.Footer.Height = 60; // add some html content to the footer PdfHtmlSection footerHtml = new PdfHtmlSection(model.FooterUrl, ""); footerHtml.AutoFitHeight = HtmlToPdfPageFitMode.AutoFit; converter.Footer.Add(footerHtml); PdfTextSection text = new PdfTextSection(0, 10, "Page: {page_number} of {total_pages} ", new System.Drawing.Font("Arial", 8)); text.HorizontalAlign = PdfTextHorizontalAlign.Right; converter.Footer.Add(text); } converter.Options.PdfPageSize = PdfPageSize.A4; converter.Options.PdfPageOrientation = PdfPageOrientation.Portrait; converter.Options.MarginLeft = 30; converter.Options.MarginRight = 30; converter.Options.MarginTop = 20; converter.Options.MarginBottom = 25; PdfDocument doc = converter.ConvertHtmlString(model.HtmlString); doc.Save(model.RepositoryPath); doc.Close(); }
/// <summary> /// This function is used to set the PDF configuration options /// 1. Header Settings /// 2. Footer Setings /// 3. Font Setting /// 4. Date time and page number in footer /// </summary> /// <param name="converter">Object of the SelectPdf class</param> public static void ConfigurePDFOptions(ref HtmlToPdf converter) { // set converter options converter.Options.PdfPageSize = PdfPageSize.A4; converter.Options.PdfPageOrientation = PdfPageOrientation.Portrait; converter.Options.MarginLeft = 10; converter.Options.MarginRight = 10; converter.Options.MarginTop = 20; converter.Options.MarginBottom = 20; // header settings converter.Options.DisplayHeader = true; converter.Header.DisplayOnFirstPage = true; converter.Header.DisplayOnOddPages = true; converter.Header.DisplayOnEvenPages = true; converter.Header.Height = 15; // Set the sample data in header PdfTextSection headerText = new PdfTextSection(4, 0, "Cloud PCR", new Font("Arial", 12)); headerText.HorizontalAlign = PdfTextHorizontalAlign.Left; headerText.VerticalAlign = PdfTextVerticalAlign.Middle; converter.Header.Add(headerText); // footer settings converter.Options.DisplayFooter = true; converter.Footer.DisplayOnFirstPage = true; converter.Footer.DisplayOnOddPages = true; converter.Footer.DisplayOnEvenPages = true; converter.Footer.Height = 20; // Set the current datetime in footer PdfTextSection footerText = new PdfTextSection(4, 0, DateTime.Now.ToString("dd-MM-yyyy HH:mm"), new Font("Arial", 8)); footerText.HorizontalAlign = PdfTextHorizontalAlign.Left; footerText.VerticalAlign = PdfTextVerticalAlign.Middle; converter.Footer.Add(footerText); // Set the page numbers in footer footerText = new PdfTextSection(4, 0, "Page: {page_number} of {total_pages} ", new Font("Arial", 8)); footerText.HorizontalAlign = PdfTextHorizontalAlign.Right; footerText.VerticalAlign = PdfTextVerticalAlign.Middle; converter.Footer.Add(footerText); }
public Byte[] ConvertToPdf(String html) { //string htmlString = html; //string baseUrl = ""; string pdf_page_size = "Custom"; PdfPageSize pageSize = (PdfPageSize)Enum.Parse(typeof(PdfPageSize), pdf_page_size, true); string pdf_orientation = "Landscape"; PdfPageOrientation pdfOrientation = (PdfPageOrientation)Enum.Parse(typeof(PdfPageOrientation), pdf_orientation, true); // instantiate a html to pdf converter object HtmlToPdf converter = new HtmlToPdf(); // set converter options converter.Options.PdfPageSize = pageSize; converter.Options.PdfPageOrientation = pdfOrientation; converter.Options.WebPageWidth = 1200; converter.Options.WebPageHeight = 0; PdfTextSection txtPageNum = new PdfTextSection(0, 10, "Page: {page_number} of {total_pages} ", new Font("Times New Roman", 10)); txtPageNum.HorizontalAlign = PdfTextHorizontalAlign.Right; converter.Footer.Add(txtPageNum); PdfTextSection txtDate = new PdfTextSection(0, 10, DateTime.Now.ToLongDateString(), new Font("Times New Roman", 10)); txtDate.HorizontalAlign = PdfTextHorizontalAlign.Left; converter.Footer.Add(txtDate); converter.Options.DisplayFooter = true; PdfDocument doc = converter.ConvertUrl(html); // save pdf document byte[] pdf = doc.Save(); // close pdf document doc.Close(); return(pdf); // return resulted pdf document }
public static void Create(string html, string filename, string baseUrl = "", string title = "") { var converter = new HtmlToPdf(); converter.Options.DrawBackground = true; converter.Options.KeepImagesTogether = true; converter.Options.EmbedFonts = true; converter.Options.MarginTop = 50; converter.Options.MarginBottom = 50; converter.Options.MarginLeft = 80; converter.Options.MarginRight = 80; converter.Options.PdfPageSize = PdfPageSize.A4; converter.Options.PdfPageOrientation = PdfPageOrientation.Portrait; converter.Options.WebPageWidth = 1024; converter.Options.WebPageHeight = 0; converter.Options.CssMediaType = HtmlToPdfCssMediaType.Screen; converter.Options.DisplayFooter = true; var footer = new PdfTextSection(0, 10, "{page_number} / {total_pages}", new Font("Arial", 8)) { HorizontalAlign = PdfTextHorizontalAlign.Right }; converter.Footer.Add(footer); // Save PDF document var doc = converter.ConvertHtmlString(html, baseUrl); doc.DocumentInformation.Title = title; doc.DocumentInformation.Author = "UI Best Practices Playbook for ASP.NET MVC"; doc.DocumentInformation.Subject = title; doc.DocumentInformation.CreationDate = DateTime.Now; doc.Save(filename); doc.Close(); }
public static void GeneratePDF(string outPath, string docBase, string outFileName, string html, string logoPrifix, string title, string footerWarning, int?totalPages) { ImageUtil.ValidateInit(); //path for the PDF file to be generated string docDir = string.Format("{0}/{1}", System.Web.Hosting.HostingEnvironment.MapPath(docBase), outPath); string outFileFullPath = string.Format("{0}/{1}", docDir, outFileName); if (System.IO.Directory.Exists(docDir)) { try { if (System.IO.File.Exists(outFileFullPath)) { System.IO.File.Delete(outFileFullPath); } } catch { } } else { System.IO.Directory.CreateDirectory(docDir); } try { HtmlToPdf converter = new HtmlToPdf(); converter.Options.PdfPageSize = PdfPageSize.A4; converter.Options.PdfPageOrientation = PdfPageOrientation.Portrait; converter.Options.MarginLeft = 30; converter.Options.MarginRight = 30; converter.Options.MarginTop = 5; converter.Options.MarginBottom = 10; converter.Options.WebPageWidth = 720; if (string.IsNullOrEmpty(footerWarning)) { converter.Options.WebPageHeight = 800; } else { converter.Options.WebPageHeight = 600; } // header settings converter.Options.DisplayHeader = true; converter.Header.DisplayOnFirstPage = true; converter.Header.DisplayOnOddPages = true; converter.Header.DisplayOnEvenPages = true; converter.Header.Height = 80; // footer settings converter.Options.DisplayFooter = true; converter.Footer.DisplayOnFirstPage = true; converter.Footer.DisplayOnOddPages = true; converter.Footer.DisplayOnEvenPages = true; if (!string.IsNullOrEmpty(footerWarning)) { converter.Footer.Height = 80; } else { converter.Footer.Height = 60; } int y = 0; if (!string.IsNullOrEmpty(footerWarning)) { PdfTextSection text2 = new PdfTextSection(0, y, footerWarning, new System.Drawing.Font("Arial", 10, System.Drawing.FontStyle.Bold)); text2.HorizontalAlign = PdfTextHorizontalAlign.Center; text2.ForeColor = System.Drawing.Color.Red; converter.Footer.Add(text2); y += 25; } PdfHtmlSection footerHtml = new PdfHtmlSection(0, y, footerContent, null); footerHtml.AutoFitWidth = HtmlToPdfPageFitMode.AutoFit; converter.Footer.Add(footerHtml); y += 40; // add page numbering element to the footer // page numbers can be added using a PdfTextSection object PdfTextSection text = new PdfTextSection(0, y, title + " Page: {page_number} of {total_pages} ", new System.Drawing.Font("Arial", 8)); text.HorizontalAlign = PdfTextHorizontalAlign.Center; converter.Footer.Add(text); // create a new pdf document converting an url PdfDocument doc = converter.ConvertHtmlString(html); // create memory stream to save PDF MemoryStream pdfStream = new MemoryStream(); // save pdf document into a MemoryStream doc.Save(pdfStream); // reset stream position pdfStream.Position = 0; // close pdf document doc.Close(); System.IO.File.WriteAllBytes(outFileFullPath, pdfStream.ToArray()); } finally { } }
private void BtnCreatePdf_Click(object sender, EventArgs e) { Cursor = Cursors.WaitCursor; string file = "Document.pdf"; try { // get parameters bool showHeaderOnFirstPage = ChkHeaderFirstPage.Checked; bool showHeaderOnOddPages = ChkHeaderOddPages.Checked; bool showHeaderOnEvenPages = ChkHeaderEvenPages.Checked; int headerHeight = 50; try { headerHeight = Convert.ToInt32(TxtHeaderHeight.Text); } catch { } bool showFooterOnFirstPage = ChkFooterFirstPage.Checked; bool showFooterOnOddPages = ChkFooterOddPages.Checked; bool showFooterOnEvenPages = ChkFooterEvenPages.Checked; int footerHeight = 50; try { footerHeight = Convert.ToInt32(TxtFooterHeight.Text); } catch { } // instantiate a html to pdf converter object HtmlToPdf converter = new HtmlToPdf(); // header settings converter.Options.DisplayHeader = showHeaderOnFirstPage || showHeaderOnOddPages || showHeaderOnEvenPages; converter.Header.DisplayOnFirstPage = showHeaderOnFirstPage; converter.Header.DisplayOnOddPages = showHeaderOnOddPages; converter.Header.DisplayOnEvenPages = showHeaderOnEvenPages; converter.Header.Height = headerHeight; PdfHtmlSection headerHtml = new PdfHtmlSection(Path.GetFullPath(headerUrl)); headerHtml.AutoFitHeight = HtmlToPdfPageFitMode.AutoFit; converter.Header.Add(headerHtml); // footer settings converter.Options.DisplayFooter = showFooterOnFirstPage || showFooterOnOddPages || showFooterOnEvenPages; converter.Footer.DisplayOnFirstPage = showFooterOnFirstPage; converter.Footer.DisplayOnOddPages = showFooterOnOddPages; converter.Footer.DisplayOnEvenPages = showFooterOnEvenPages; converter.Footer.Height = footerHeight; PdfHtmlSection footerHtml = new PdfHtmlSection(Path.GetFullPath(footerUrl)); footerHtml.AutoFitHeight = HtmlToPdfPageFitMode.AutoFit; converter.Footer.Add(footerHtml); // add page numbering element to the footer if (ChkPageNumbering.Checked) { // page numbers can be added using a PdfTextSection object PdfTextSection text = new PdfTextSection(0, 10, "Page: {page_number} of {total_pages} ", new System.Drawing.Font("Arial", 8)); text.HorizontalAlign = PdfTextHorizontalAlign.Right; converter.Footer.Add(text); } // create a new pdf document converting an url PdfDocument doc = converter.ConvertUrl(TxtUrl.Text); // save pdf document doc.Save(file); // close pdf document doc.Close(); } catch (Exception ex) { MessageBox.Show(String.Format("Error: {0}", ex.Message), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } finally { Cursor = Cursors.Arrow; } // open generated pdf try { System.Diagnostics.Process.Start(file); } catch { MessageBox.Show("Could not open generated pdf document", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
protected void BtnCreatePdf_Click(object sender, EventArgs e) { // get parameters string headerUrl = Server.MapPath("~/files/header.html"); string footerUrl = Server.MapPath("~/files/footer.html"); bool showHeaderOnFirstPage = ChkHeaderFirstPage.Checked; bool showHeaderOnOddPages = ChkHeaderOddPages.Checked; bool showHeaderOnEvenPages = ChkHeaderEvenPages.Checked; int headerHeight = 50; try { headerHeight = Convert.ToInt32(TxtHeaderHeight.Text); } catch { } bool showFooterOnFirstPage = ChkFooterFirstPage.Checked; bool showFooterOnOddPages = ChkFooterOddPages.Checked; bool showFooterOnEvenPages = ChkFooterEvenPages.Checked; int footerHeight = 50; try { footerHeight = Convert.ToInt32(TxtFooterHeight.Text); } catch { } // instantiate a html to pdf converter object HtmlToPdf converter = new HtmlToPdf(); // header settings converter.Options.DisplayHeader = showHeaderOnFirstPage || showHeaderOnOddPages || showHeaderOnEvenPages; converter.Header.DisplayOnFirstPage = showHeaderOnFirstPage; converter.Header.DisplayOnOddPages = showHeaderOnOddPages; converter.Header.DisplayOnEvenPages = showHeaderOnEvenPages; converter.Header.Height = headerHeight; PdfHtmlSection headerHtml = new PdfHtmlSection(headerUrl); headerHtml.AutoFitHeight = HtmlToPdfPageFitMode.AutoFit; converter.Header.Add(headerHtml); // footer settings converter.Options.DisplayFooter = showFooterOnFirstPage || showFooterOnOddPages || showFooterOnEvenPages; converter.Footer.DisplayOnFirstPage = showFooterOnFirstPage; converter.Footer.DisplayOnOddPages = showFooterOnOddPages; converter.Footer.DisplayOnEvenPages = showFooterOnEvenPages; converter.Footer.Height = footerHeight; PdfHtmlSection footerHtml = new PdfHtmlSection(footerUrl); footerHtml.AutoFitHeight = HtmlToPdfPageFitMode.AutoFit; converter.Footer.Add(footerHtml); // add page numbering element to the footer if (ChkPageNumbering.Checked) { // page numbers can be added using a PdfTextSection object PdfTextSection text = new PdfTextSection(0, 10, "Page: {page_number} of {total_pages} ", new System.Drawing.Font("Arial", 8)); text.HorizontalAlign = PdfTextHorizontalAlign.Right; converter.Footer.Add(text); } // create a new pdf document converting an url PdfDocument doc = converter.ConvertUrl(TxtUrl.Text); // save pdf document doc.Save(Response, false, "Sample.pdf"); // close pdf document doc.Close(); }
public String PDFReport(string modelo, string pIdSolicitante, string pIDGenerado, string fechaSolicitud) { Response.CacheControl = "no-cache"; System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("es-CO"); //string urlbase = Url.Action("Oficio", "OficioSolicitudNovedades"); //var fullUrl = this.Url.Action("ConstanciaTipo", "OficioSolicitudNovedades", new { id = id }, "http"); var fullUrl = this.Url.Action("ConstanciaSAAH", "GeneracionPdfs", new { hogarcodigo = "1111" }, "http"); //this.Request.Url.Scheme remplazado por "https" //string headerUrl = this.Url.Action("HeaderUrl", "GeneracionPdfs", new { IDGenerado = pIDGenerado }, "http"); //string footerUrl = this.Url.Action("FooterUrl", "GeneracionPdfs", new { IDGenerado = pIDGenerado }, "http"); string headerUrl = this.Url.Action("HeaderUrl", "GeneracionPdfs", "http"); string footerUrl = this.Url.Action("FooterUrl", "GeneracionPdfs", "http"); // CREACION DE PDF CON DLL GNU SelectPdf.GlobalProperties.LicenseKey = "1f7k9efg5PXm5+Tm9eTt++X15uT75Of77Ozs7A=="; string pdf_page_size = "Letter"; PdfPageSize pageSize = (PdfPageSize)Enum.Parse(typeof(PdfPageSize), pdf_page_size, true); string pdf_orientation = "portrait"; PdfPageOrientation pdfOrientation = (PdfPageOrientation)Enum.Parse(typeof(PdfPageOrientation), pdf_orientation, true); int webPageWidth = 1024; try { webPageWidth = System.Convert.ToInt32(webPageWidth); } catch { } int webPageHeight = 0; try { webPageHeight = System.Convert.ToInt32(webPageHeight); } catch { } // try { // instantiate a html to pdf converter object HtmlToPdf converter = new HtmlToPdf(); converter.Options.SecureProtocol = 0; // set converter options converter.Options.PdfPageSize = pageSize; converter.Options.PdfPageOrientation = pdfOrientation; converter.Options.WebPageWidth = webPageWidth; converter.Options.WebPageHeight = webPageHeight; converter.Options.MarginLeft = 20; //converter.Options.MarginRight = 20; // header settings converter.Options.DisplayHeader = true; converter.Header.DisplayOnFirstPage = true; converter.Header.DisplayOnOddPages = true; converter.Header.DisplayOnEvenPages = true; converter.Header.Height = 80; // add some html content to the header PdfHtmlSection headerHtml = new PdfHtmlSection(headerUrl); headerHtml.AutoFitHeight = HtmlToPdfPageFitMode.AutoFit; converter.Header.Add(headerHtml); // footer settings converter.Options.DisplayFooter = true; converter.Footer.DisplayOnFirstPage = true; converter.Footer.DisplayOnOddPages = true; converter.Footer.DisplayOnEvenPages = true; converter.Footer.Height = 50; // add some html content to the footer PdfHtmlSection footerHtml = new PdfHtmlSection(footerUrl); footerHtml.AutoFitHeight = HtmlToPdfPageFitMode.AutoFit; converter.Footer.Add(footerHtml); // page numbers can be added using a PdfTextSection object //PdfTextSection text = new PdfTextSection(0, 20, "Código Verificación: "+ pIDGenerado.ToString() + " Pag: {page_number} de {total_pages} Fecha: " + fechaSolicitud , new System.Drawing.Font("Verdana", 7)); PdfTextSection text = new PdfTextSection(0, 20, "Pag: {page_number} de {total_pages} Fecha: " + fechaSolicitud + " Código Verificación: " + pIDGenerado.ToString(), new System.Drawing.Font("Verdana", 7)); //string cusomtSwitches = string.Format("--header-html {0} --footer-right \"Fecha: {1}\" " + "--footer-left \"Codigo Verificacion: {2}\" " + "--footer-center \"Pag: [page] de [toPage]\" --footer-line --footer-font-size \"9\" --footer-spacing 5 --footer-font-name \"calibri light\" ", Url.Action("Footer", "ConsultaIndividual", new { IDGenerado = pIdGenerado.ToString() }, this.Request.Url.Scheme), fechaSolicitud, pIdGenerado.ToString()); text.HorizontalAlign = PdfTextHorizontalAlign.Default; converter.Footer.Add(text); // specify the number of seconds the conversion is delayed converter.Options.MinPageLoadTime = 10; // set the page timeout (in seconds) converter.Options.MaxPageLoadTime = 300; // create a new pdf document converting an url PdfDocument doc = converter.ConvertUrl(fullUrl); //save the file to server temp folder string fileName = "ConstanciaSAAH_" + pIDGenerado + ".pdf"; string fullPath = ConfigurationManager.AppSettings["RutaArchivoConstancia"] + fileName; // save pdf document doc.Save(fullPath); doc.Close(); Session["NombrePDF"] = fileName; return("1"); }
public ActionResult ConstanciaSAAHE(String hogarcodigo) { if (hogarcodigo == null) { hogarcodigo = "8HI49"; } Response.CacheControl = "no-cache"; System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("es-CO"); var fullUrl = this.Url.Action("ConstanciaSAAH", "GeneracionPdfs", new { hogarcodigo = hogarcodigo }, "http"); string headerUrl = this.Url.Action("HeaderUrl", "GeneracionPdfs", null, "http"); string footerUrl = this.Url.Action("FooterUrl", "GeneracionPdfs", null, "http"); SelectPdf.GlobalProperties.LicenseKey = "1f7k9efg5PXm5+Tm9eTt++X15uT75Of77Ozs7A=="; string pdf_page_size = "Letter"; PdfPageSize pageSize = (PdfPageSize)Enum.Parse(typeof(PdfPageSize), pdf_page_size, true); string pdf_orientation = "portrait"; PdfPageOrientation pdfOrientation = (PdfPageOrientation)Enum.Parse(typeof(PdfPageOrientation), pdf_orientation, true); int webPageWidth = 1024; try { webPageWidth = System.Convert.ToInt32(webPageWidth); } catch (Exception e) { Console.WriteLine(e.Message); } int webPageHeight = 0; try { webPageHeight = System.Convert.ToInt32(webPageHeight); } catch (Exception e) { Console.WriteLine(e.Message); } // try { // instantiate a html to pdf converter object HtmlToPdf converter = new HtmlToPdf(); converter.Options.SecureProtocol = 0; // set converter options converter.Options.PdfPageSize = pageSize; converter.Options.PdfPageOrientation = pdfOrientation; converter.Options.WebPageWidth = webPageWidth; converter.Options.WebPageHeight = webPageHeight; converter.Options.MarginLeft = 20; //converter.Options.MarginRight = 20; // header settings converter.Options.DisplayHeader = true; converter.Header.DisplayOnFirstPage = true; converter.Header.DisplayOnOddPages = true; converter.Header.DisplayOnEvenPages = true; converter.Header.Height = 80; // add some html content to the header PdfHtmlSection headerHtml = new PdfHtmlSection(headerUrl); headerHtml.AutoFitHeight = HtmlToPdfPageFitMode.AutoFit; converter.Header.Add(headerHtml); // footer settings converter.Options.DisplayFooter = true; converter.Footer.DisplayOnFirstPage = true; converter.Footer.DisplayOnOddPages = true; converter.Footer.DisplayOnEvenPages = true; converter.Footer.Height = 50; // add some html content to the footer PdfHtmlSection footerHtml = new PdfHtmlSection(footerUrl); footerHtml.AutoFitHeight = HtmlToPdfPageFitMode.AutoFit; converter.Footer.Add(footerHtml); PdfTextSection text = new PdfTextSection(0, 20, "Pag: {page_number} de {total_pages} Fecha: " + "04/12/194" + " Código Verificación: " + "prueba".ToString(), new System.Drawing.Font("Verdana", 7)); text.HorizontalAlign = PdfTextHorizontalAlign.Default; // specify the number of seconds the conversion is delayed converter.Options.MinPageLoadTime = 2;/*120*/ // set the page timeout (in seconds) converter.Options.MaxPageLoadTime = 300; // create a new pdf document converting an url PdfDocument doc = new PdfDocument(); try { doc = converter.ConvertUrl(fullUrl); //save the file to server temp folder string fileName = "ConstanciaSAAH_" + hogarcodigo + ".pdf"; string fullPath = ConfigurationManager.AppSettings["RutaArchivoConstancias"] + fileName; // save pdf document doc.Save(fullPath); doc.Close(); Session["NombrePDF"] = fileName; } catch (Exception e) { Console.WriteLine(e.Message); } return(RedirectToAction("Inicio", "ConformacionHogar")); }
protected void BtnCreatePdf_Click(object sender, EventArgs e) { // get parameters string headerUrl = Server.MapPath("~/files/header.html"); string footerUrl = Server.MapPath("~/files/footer.html"); bool showHeaderOnFirstPage = ChkHeaderFirstPage.Checked; bool showHeaderOnOddPages = ChkHeaderOddPages.Checked; bool showHeaderOnEvenPages = ChkHeaderEvenPages.Checked; int headerHeight = 50; try { headerHeight = Convert.ToInt32(TxtHeaderHeight.Text); } catch { } bool showFooterOnFirstPage = ChkFooterFirstPage.Checked; bool showFooterOnOddPages = ChkFooterOddPages.Checked; bool showFooterOnEvenPages = ChkFooterEvenPages.Checked; int footerHeight = 50; try { footerHeight = Convert.ToInt32(TxtFooterHeight.Text); } catch { } // instantiate a html to pdf converter object HtmlToPdf converter = new HtmlToPdf(); // header settings converter.Options.DisplayHeader = showHeaderOnFirstPage || showHeaderOnOddPages || showHeaderOnEvenPages; converter.Header.DisplayOnFirstPage = showHeaderOnFirstPage; converter.Header.DisplayOnOddPages = showHeaderOnOddPages; converter.Header.DisplayOnEvenPages = showHeaderOnEvenPages; converter.Header.Height = headerHeight; PdfHtmlSection headerHtml = new PdfHtmlSection(headerUrl); headerHtml.AutoFitHeight = HtmlToPdfPageFitMode.AutoFit; converter.Header.Add(headerHtml); // footer settings converter.Options.DisplayFooter = showFooterOnFirstPage || showFooterOnOddPages || showFooterOnEvenPages; converter.Footer.DisplayOnFirstPage = showFooterOnFirstPage; converter.Footer.DisplayOnOddPages = showFooterOnOddPages; converter.Footer.DisplayOnEvenPages = showFooterOnEvenPages; converter.Footer.Height = footerHeight; PdfHtmlSection footerHtml = new PdfHtmlSection(footerUrl); footerHtml.AutoFitHeight = HtmlToPdfPageFitMode.AutoFit; converter.Footer.Add(footerHtml); // add page numbering element to the footer if (ChkPageNumbering.Checked) { // page numbers can be added using a PdfTextSection object PdfTextSection text = new PdfTextSection(0, 10, "Page: {page_number} of {total_pages} ", new System.Drawing.Font("Arial", 8)); text.HorizontalAlign = PdfTextHorizontalAlign.Right; converter.Footer.Add(text); } // create a new pdf document converting an url PdfDocument doc = converter.ConvertUrl(TxtUrl.Text); // save pdf document doc.Save(Response, false, "Sample.pdf"); // close pdf document doc.Close(); }
static void Main(string[] args) { if (args.Length < 1) { PrintUsage(); } var option = args[0]; var inputfilepath = string.Empty; var outputfilepath = string.Empty; var xsltPath = string.Empty; FileStream fsIn; TextReader textReader; AppContext.SetSwitch("Switch.System.Xml.AllowDefaultResolver", true); try { switch (option) { case "tohtml": if (args.Length != 4) { PrintUsage(); break; } Console.WriteLine("Beginning conversion from XML to HTML..."); inputfilepath = args[1]; outputfilepath = args[2]; xsltPath = args[3]; fsIn = new FileStream(inputfilepath, FileMode.Open); textReader = new StreamReader(fsIn); XmlDocument xmlDocument = new XmlDocument(); xmlDocument.LoadXml(textReader.ReadToEnd()); XslCompiledTransform xslCompiledTransform = new XslCompiledTransform(); XsltArgumentList xsltArgumentList = new XsltArgumentList(); XsltSettings settings = new XsltSettings(true, false); FileStream fs = new FileStream(outputfilepath, FileMode.Create, FileAccess.Write); TextWriter writer = new StreamWriter(fs); xslCompiledTransform.Load(xsltPath, settings, new XmlUrlResolver()); xslCompiledTransform.Transform(xmlDocument, xsltArgumentList, writer); fs.Close(); Console.WriteLine("Converted to HTML"); break; case "topdf": if (args.Length != 3) { PrintUsage(); break; } Console.WriteLine("Beginning conversion from HTML to PDF..."); inputfilepath = args[1]; outputfilepath = args[2]; fsIn = new FileStream(inputfilepath, FileMode.Open); textReader = new StreamReader(fsIn); // instantiate the html to pdf converter HtmlToPdf converter = new HtmlToPdf(); converter.Options.PdfPageSize = PdfPageSize.Letter; converter.Options.PdfPageOrientation = PdfPageOrientation.Portrait; converter.Options.CssMediaType = HtmlToPdfCssMediaType.Print; converter.Options.MarginLeft = 10; converter.Options.MarginRight = 10; converter.Options.MarginTop = 2; converter.Options.MarginBottom = 2; // header settings converter.Options.DisplayHeader = true; converter.Header.DisplayOnFirstPage = true; converter.Header.DisplayOnOddPages = true; converter.Header.DisplayOnEvenPages = true; converter.Header.Height = 30; // footer settings converter.Options.DisplayFooter = true; converter.Footer.DisplayOnFirstPage = true; converter.Footer.DisplayOnOddPages = true; converter.Footer.DisplayOnEvenPages = true; converter.Footer.Height = 30; // add some html content to the footer PdfHtmlSection headerHtml = new PdfHtmlSection(@"<div style=""text-align: right; width: 100%; font-size: 8pt"">Rendered by/Généré par CanPESC</div>", ""); converter.Header.Add(headerHtml); // page numbers can be added using a PdfTextSection object PdfTextSection text = new PdfTextSection(-10, 10, "Page {page_number} of/de {total_pages} ", new System.Drawing.Font("Arial", 8)); text.HorizontalAlign = PdfTextHorizontalAlign.Right; converter.Footer.Add(text); PdfTextSection text2 = new PdfTextSection(10, 10, "This is not an official transcript | Ceci n'est pas une relevée de notes officielle", new System.Drawing.Font("Arial", 8)); text2.HorizontalAlign = PdfTextHorizontalAlign.Left; converter.Footer.Add(text2); // convert the url to pdf PdfDocument doc = converter.ConvertHtmlString(textReader.ReadToEnd()); // PDF Options - can be configured doc.DocumentInformation.Title = "College Transcript"; doc.DocumentInformation.Author = "CanPESC"; // save pdf document doc.Save(outputfilepath); // close pdf document doc.Close(); Console.WriteLine("Converted to PDF"); break; default: PrintUsage(); break; } } catch (Exception ex) { Console.WriteLine($"An error occurred: {ex.Message}"); PrintUsage(); } }
public IActionResult OnPost() { // get parameters string headerUrl = Startup.ServerPath + @"\wwwroot\files\header.html"; string footerUrl = Startup.ServerPath + @"\wwwroot\files\footer.html"; // instantiate a html to pdf converter object HtmlToPdf converter = new HtmlToPdf(); // header settings converter.Options.DisplayHeader = showHeaderOnFirstPage || showHeaderOnOddPages || showHeaderOnEvenPages; converter.Header.DisplayOnFirstPage = showHeaderOnFirstPage; converter.Header.DisplayOnOddPages = showHeaderOnOddPages; converter.Header.DisplayOnEvenPages = showHeaderOnEvenPages; converter.Header.Height = headerHeight; PdfHtmlSection headerHtml = new PdfHtmlSection(headerUrl); headerHtml.AutoFitHeight = HtmlToPdfPageFitMode.AutoFit; converter.Header.Add(headerHtml); // footer settings converter.Options.DisplayFooter = showFooterOnFirstPage || showFooterOnOddPages || showFooterOnEvenPages; converter.Footer.DisplayOnFirstPage = showFooterOnFirstPage; converter.Footer.DisplayOnOddPages = showFooterOnOddPages; converter.Footer.DisplayOnEvenPages = showFooterOnEvenPages; converter.Footer.Height = footerHeight; PdfHtmlSection footerHtml = new PdfHtmlSection(footerUrl); footerHtml.AutoFitHeight = HtmlToPdfPageFitMode.AutoFit; converter.Footer.Add(footerHtml); // add page numbering element to the footer if (showPageNumbering) { // page numbers can be added using a PdfTextSection object PdfTextSection text = new PdfTextSection(0, 10, "Page: {page_number} of {total_pages} ", new System.Drawing.Font("Arial", 8)); text.HorizontalAlign = PdfTextHorizontalAlign.Right; converter.Footer.Add(text); } // create a new pdf document converting an url PdfDocument doc = converter.ConvertUrl(TxtUrl); // save pdf document byte[] pdf = doc.Save(); // close pdf document doc.Close(); // return resulted pdf document FileResult fileResult = new FileContentResult(pdf, "application/pdf"); fileResult.FileDownloadName = "Document.pdf"; return(fileResult); }
public ActionResult SubmitAction(FormCollection collection) { // get parameters string headerUrl = Server.MapPath("~/files/header.html"); string footerUrl = Server.MapPath("~/files/footer.html"); bool showHeaderOnFirstPage = collection["ChkHeaderFirstPage"] == "on"; bool showHeaderOnOddPages = collection["ChkHeaderOddPages"] == "on"; bool showHeaderOnEvenPages = collection["ChkHeaderEvenPages"] == "on"; int headerHeight = 50; try { headerHeight = Convert.ToInt32(collection["TxtHeaderHeight"]); } catch { } bool showFooterOnFirstPage = collection["ChkFooterFirstPage"] == "on"; bool showFooterOnOddPages = collection["ChkFooterOddPages"] == "on"; bool showFooterOnEvenPages = collection["ChkFooterEvenPages"] == "on"; int footerHeight = 50; try { footerHeight = Convert.ToInt32(collection["TxtFooterHeight"]); } catch { } // instantiate a html to pdf converter object HtmlToPdf converter = new HtmlToPdf(); // header settings converter.Options.DisplayHeader = showHeaderOnFirstPage || showHeaderOnOddPages || showHeaderOnEvenPages; converter.Header.DisplayOnFirstPage = showHeaderOnFirstPage; converter.Header.DisplayOnOddPages = showHeaderOnOddPages; converter.Header.DisplayOnEvenPages = showHeaderOnEvenPages; converter.Header.Height = headerHeight; PdfHtmlSection headerHtml = new PdfHtmlSection(headerUrl); headerHtml.AutoFitHeight = HtmlToPdfPageFitMode.AutoFit; converter.Header.Add(headerHtml); // footer settings converter.Options.DisplayFooter = showFooterOnFirstPage || showFooterOnOddPages || showFooterOnEvenPages; converter.Footer.DisplayOnFirstPage = showFooterOnFirstPage; converter.Footer.DisplayOnOddPages = showFooterOnOddPages; converter.Footer.DisplayOnEvenPages = showFooterOnEvenPages; converter.Footer.Height = footerHeight; PdfHtmlSection footerHtml = new PdfHtmlSection(footerUrl); footerHtml.AutoFitHeight = HtmlToPdfPageFitMode.AutoFit; converter.Footer.Add(footerHtml); // add page numbering element to the footer if (collection["ChkPageNumbering"] == "on") { // page numbers can be added using a PdfTextSection object PdfTextSection text = new PdfTextSection(0, 10, "Page: {page_number} of {total_pages} ", new System.Drawing.Font("Arial", 8)); text.HorizontalAlign = PdfTextHorizontalAlign.Right; converter.Footer.Add(text); } // create a new pdf document converting an url PdfDocument doc = converter.ConvertUrl(collection["TxtUrl"]); // custom header on page 3 if (doc.Pages.Count >= 3) { PdfPage page = doc.Pages[2]; PdfTemplate customHeader = doc.AddTemplate( page.PageSize.Width, headerHeight); PdfHtmlElement customHtml = new PdfHtmlElement( "<div><b>This is the custom header that will " + "appear only on page 3!</b></div>", string.Empty); customHeader.Add(customHtml); page.CustomHeader = customHeader; } // save pdf document byte[] pdf = doc.Save(); // close pdf document doc.Close(); // return resulted pdf document FileResult fileResult = new FileContentResult(pdf, "application/pdf"); fileResult.FileDownloadName = "Document.pdf"; return(fileResult); }