public static async Task CreatePdfFrumUrl(string url, string fileName) { PdfPageSize pageSize = PdfPageSize.A4; PdfPageOrientation pdfOrientation = PdfPageOrientation.Portrait; int webPageWidth = 1024; int webPageHeight = 1000; // 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 = webPageWidth; converter.Options.WebPageHeight = webPageHeight; // create a new pdf document converting an url PdfDocument doc = converter.ConvertUrl(url); // save pdf document doc.Save($@"C:\Temp\{fileName}.pdf"); // close pdf document doc.Close(); }
protected void btnMakePDF_Click(object sender, EventArgs e) { string url = txtMakePDF.Value; string pdf_page_size = "A4"; PdfPageSize pageSize = (PdfPageSize)Enum.Parse(typeof(PdfPageSize), pdf_page_size, true); PdfPageOrientation pdfOrientation = (PdfPageOrientation)Enum.Parse(typeof(PdfPageOrientation), "Portrait", true); HtmlToPdf converter = new HtmlToPdf(); converter.Options.PdfPageSize = pageSize; converter.Options.PdfPageOrientation = pdfOrientation; converter.Options.MarginBottom = 10; converter.Options.MarginLeft = 10; converter.Options.MarginRight = 10; converter.Options.MarginTop = 10; try { PdfDocument doc = converter.ConvertUrl(url); // if you want to save to file doc.Save(Server.MapPath("/Documents/document3.pdf")); doc.Save(Response, true, "document3.pdf"); doc.Close(); } catch (Exception ex) { // show the error model if (ex.Message != "Thread was being aborted.") { myModal.Style.Add("display", "block"); } } }
/// <summary> /// Adds new page to document /// </summary> /// <returns>Page</returns> public PdfPage AddPage(PdfUnit unit) { unit = unit ?? Unit ?? PdfUnit.Default; var size = PageSize != null?PageSize.ChangeUnits(unit) : PdfPageSize.Default(unit); return(AddPage(size)); }
public Byte[] ExportDrawing(string html) { Byte[] res = null; string htmlString = html; PdfPageSize pageSize = PdfPageSize.A4; PdfPageOrientation pdfOrientation = PdfPageOrientation.Landscape; HtmlToPdf converter = new HtmlToPdf(); converter.Options.PdfPageSize = pageSize; converter.Options.PdfPageOrientation = pdfOrientation; PdfDocument doc = converter.ConvertHtmlString(htmlString); using (MemoryStream ms = new MemoryStream()) { doc.Save(ms); res = ms.ToArray(); doc.Close(); } return(res); }
static void Main(string[] args) { //HelloWorld.Generate(); //DocProperties.Create("docProperties.pdf"); //LinearizedPdf.Create("linearized.pdf"); PdfPageSize.Create("pdfPageSize.pdf"); }
protected void btnPDF_Click(object sender, EventArgs e) { string url = @"http://localhost/MobileDeviceManagement/Form.aspx?id=" + formId + "&hideBtn=true"; string pdf_page_size = "A4"; 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; //int webPageHeight = 0; // 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 = webPageWidth; //converter.Options.WebPageHeight = webPageHeight; // create a new pdf document converting an url PdfDocument doc = converter.ConvertUrl(url); // save pdf document doc.Save(Response, false, "Form " + formId + ".pdf"); // close pdf document doc.Close(); }
private void button4_Click(object sender, EventArgs e) { var document = new PdfDocument(); document.Fonts.Add(PdfFont.Courier); var sizes = new[] { Tuple.Create("Letter", PdfPageSize.Letter()), Tuple.Create("A0", PdfPageSize.A0()), Tuple.Create("A1", PdfPageSize.A1()), Tuple.Create("A2", PdfPageSize.A2()), Tuple.Create("A3", PdfPageSize.A3()), Tuple.Create("A4", PdfPageSize.A4()), Tuple.Create("A5", PdfPageSize.A5()), Tuple.Create("B4", PdfPageSize.B4()), Tuple.Create("B5", PdfPageSize.B5()), Tuple.Create("Custom", new PdfSize(PdfUnit.Default, 100, 50)) }; foreach (var size in sizes) { var page = document.AddPage(size.Item2); page.AddRectangle(0, 0, size.Item2.Width, size.Item2.Height, PdfColor.White, 2.0F, PdfColor.Red); var text = page.AddText(string.Format("{0}: w={1} h={2}", size.Item1, size.Item2.Width, size.Item2.Height), 10, PdfFont.Courier); text.X = 0; text.Y = size.Item2.Height - 20; } document.Save(@"test.pdf"); Process.Start(@"test.pdf"); }
protected void Button_Descargar_Click(object sender, EventArgs e) { UsuarioCompleto UsuarioCompleto = (UsuarioCompleto)Session["UsuarioCompleto"]; DateTime hoy = DateTime.Now; string host = HttpContext.Current.Request.Url.Host; int port = HttpContext.Current.Request.Url.Port; // read parameters from the webpage string url = host + ":" + port + "/Pages/ReportesPDF.aspx?id=" + UsuarioCompleto.Usuario.id_usuario; string pdf_page_size = "A4"; 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 = Convert.ToInt32(1024); } catch { } int webPageHeight = 0; try { webPageHeight = Convert.ToInt32(0); } catch { } // 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 = webPageWidth; converter.Options.WebPageHeight = webPageHeight; // esto es re importante // lo que hago es setearle un delay de X segundos // para que se puedan generar los graficos normalmente converter.Options.MinPageLoadTime = 5; // create a new pdf document converting an url PdfDocument doc = converter.ConvertUrl(url); // save pdf document doc.Save(Response, false, "Informe_Nutricloud_" + hoy.ToString("dd-MM-yyyy") + ".pdf"); // close pdf document doc.Close(); }
// POST: contents/Delete/5 // select pdf library public FileStreamResult DownloadZip(string id) { MemoryStream workStream = new MemoryStream(); content content = db.contents.Find(id); // read parameters from the webpage string htmlString = content.content_post; string pdf_page_size = "A4"; 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 = 1024; } catch { } int webPageHeight = 0; try { webPageHeight = 0; } catch { } // 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 = webPageWidth; converter.Options.WebPageHeight = webPageHeight; // create a new pdf document converting an url SelectPdf.PdfDocument doc = converter.ConvertHtmlString(htmlString); byte[] pdf = doc.Save(); doc.Close(); // put pdf into zip using (var zip = new ZipFile()) { zip.AddEntry(content.content_id + ".pdf", pdf); zip.Save(workStream); } workStream.Position = 0; FileStreamResult fileResult = new FileStreamResult(workStream, System.Net.Mime.MediaTypeNames.Application.Zip); fileResult.FileDownloadName = content.content_id + ".zip"; return(fileResult); }
private byte[] HtmlToPdf(string htmlBody, string htmlFooter) { // get parameters string headerUrl = Startup.ServerPath + @"\wwwroot\templates\header.html"; // instantiate a html to pdf converter object HtmlToPdf converter = new HtmlToPdf(); // header settings converter.Options.DisplayHeader = true; converter.Header.Height = 20; PdfHtmlSection headerHtml = new PdfHtmlSection(headerUrl) { AutoFitHeight = HtmlToPdfPageFitMode.AutoFit }; converter.Header.Add(headerHtml); // footer settings converter.Options.DisplayFooter = true; converter.Footer.Height = 165; PdfHtmlSection footerHtml = new PdfHtmlSection(htmlFooter, null) { AutoFitHeight = HtmlToPdfPageFitMode.AutoFit, AutoFitWidth = HtmlToPdfPageFitMode.AutoFit, WebPageWidth = 793 }; converter.Footer.Add(footerHtml); // read parameters from the webpage PdfPageSize pageSize = PdfPageSize.A4; PdfPageOrientation pdfOrientation = PdfPageOrientation.Portrait; int webPageWidth = 793; int webPageHeight = 0; // set converter options converter.Options.PdfPageSize = pageSize; converter.Options.PdfPageOrientation = pdfOrientation; converter.Options.WebPageWidth = webPageWidth; converter.Options.WebPageHeight = webPageHeight; converter.Options.MarginLeft = 0; converter.Options.MarginRight = 0; //set document permissions converter.Options.SecurityOptions.CanAssembleDocument = false; converter.Options.SecurityOptions.CanCopyContent = true; converter.Options.SecurityOptions.CanEditAnnotations = false; converter.Options.SecurityOptions.CanEditContent = false; converter.Options.SecurityOptions.CanPrint = true; converter.Options.PdfDocumentInformation.Title = "CPD Activity Accreditation"; converter.Options.PdfDocumentInformation.CreationDate = DateTime.UtcNow; converter.Options.PdfDocumentInformation.Subject = "CPD Activity Accreditation"; converter.Options.PdfDocumentInformation.Keywords = "scfhs.org.sa"; // create a new pdf document converting an url SelectPdf.PdfDocument doc = converter.ConvertHtmlString(htmlBody); // save pdf document byte[] pdf = doc.Save(); // close pdf document doc.Close(); // return resulted pdf document return(pdf); }
// need to do some sort of caching by retrievingthe file only if it has been converted before. public string ConvertToPdf(MatchModel match, string url) { try { var fileName = $"{match.TeamA} vs {match.TeamB} - {match.Score}.pdf"; fileName = stripIllegalCharacters(fileName); var filePath = Path.Combine(AppState.ScoresheetRepo, fileName); if (checkIfFileExists(filePath)) { return(File.ReadAllBytes(filePath).ToBase64()); } else { Logger.LogDebug($@"Converting {url} to PDF..."); string pdf_page_size = "A4"; 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; int webPageHeight = 0; // 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 = webPageWidth; converter.Options.WebPageHeight = webPageHeight; PdfDocument doc = null; // create a new pdf document converting an url doc = converter.ConvertUrl(url); Logger.LogDebug($"Saving '{filePath}'..."); // save pdf document doc.Save(filePath); Logger.LogDebug($"'{filePath}' Saved!"); // close pdf document doc.Close(); Logger.LogDebug($"Closing '{filePath}'..."); Logger.LogDebug($"Converting File Bytes to Base64..."); var base64 = File.ReadAllBytes(filePath).ToBase64(); Logger.LogDebug($"Done!"); return(base64); } } catch (Exception ex) { ex.CustomLog(Logger, "Failed to convert HTML to PDF and save the file."); throw; } }
protected void lnkDownload_Click(object sender, EventArgs e) { LinkButton lnkDownload = (LinkButton)sender; lnkDownload.Enabled = false; DataTable dt = (DataTable)ViewState["TestKey"]; string testkey = dt.Rows[0]["TestKey"].ToString(); string currenturl = HttpContext.Current.Request.Url.AbsoluteUri; currenturl = currenturl.Split('/')[2]; string url = "http://" + currenturl + "/Teacher/" + "Report.aspx?TestAssignedId=" + lnkDownload.CommandArgument.ToString(); string pdf_page_size = "A4"; PdfPageSize pageSize = (PdfPageSize)Enum.Parse(typeof(PdfPageSize), pdf_page_size, true); string pdf_orientation = "Landscape"; // string pdf_orientation = "Portrait"; PdfPageOrientation pdfOrientation = (PdfPageOrientation)Enum.Parse(typeof(PdfPageOrientation), pdf_orientation, true); int webPageWidth = 1120; // int webPageWidth = 1024; try { // webPageWidth = Convert.ToInt32(1024); webPageWidth = Convert.ToInt32(1120); } catch { } int webPageHeight = 0; try { webPageHeight = Convert.ToInt32(0); } catch { } // 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 = webPageWidth; converter.Options.WebPageHeight = webPageHeight; // create a new pdf document converting an url SelectPdf.PdfDocument doc = converter.ConvertUrl(url); // save pdf document doc.Save(Response, false, lnkDownload.CommandName + "_" + testkey + ".pdf"); // close pdf document doc.Close(); lnkDownload.Enabled = true; }
public void pdf() { string htmlString = ""; var file = (System.Web.HttpContext.Current.Server.MapPath("~/") + "practice-hours.html"); var html = "<TR VALIGN=TOP><TD WIDTH=75 STYLE='border-top: 3.00pt solid #6773b6; border-bottom: 3.00pt solid #6773b6; border-left: 3.00pt solid #6773b6; border-right: none; padding-top: 0in; padding-bottom: 0in; padding-left: 0.08in; padding-right: 0in'>" + "<P CLASS='western' STYLE='widows: 0; orphans: 0;padding-top:12px;'><FONT COLOR='#5162a5'><FONT FACE='Arial, sans-serif'><FONT SIZE=2><SPAN LANG='en-GB'><B>Dates:</B></SPAN></FONT></FONT></FONT></P></TD>" + "<TD WIDTH=115 STYLE='border-top: 3.00pt solid #6773b6; border-bottom: 3.00pt solid #6773b6; border-left: 3.00pt solid #6773b6; border-right: none; padding-top: 0in; padding-bottom: 0in; padding-left: 0.08in; padding-right: 0in'>" + "<P CLASS='western' STYLE='widows: 0; orphans: 0;padding-top:12px;'><FONT COLOR='#5162a5'><FONT FACE='Arial, sans-serif'><FONT SIZE=2><SPAN LANG='en-GB'><B>Name and address of <BR> organisation:</B></SPAN></FONT></FONT></FONT></P></TD>" + "<TD WIDTH=115 STYLE='border-top: 3.00pt solid #6773b6; border-bottom: 3.00pt solid #6773b6; border-left: 3.00pt solid #6773b6; border-right: none; padding-top: 0in; padding-bottom: 0in; padding-left: 0.08in; padding-right: 0in'>" + "<P CLASS='western' STYLE='widows: 0; orphans: 0;padding-top:12px;'><FONT COLOR='#5162a5'><FONT FACE='Arial, sans-serif'><FONT SIZE=2><SPAN LANG='en-GB'><B>Name and address of <BR> organisation:</B></SPAN></FONT></FONT></FONT></P></TD>" + "<TD WIDTH=115 STYLE='border-top: 3.00pt solid #6773b6; border-bottom: 3.00pt solid #6773b6; border-left: 3.00pt solid #6773b6; border-right: none; padding-top: 0in; padding-bottom: 0in; padding-left: 0.08in; padding-right: 0in'>" + "<P CLASS='western' STYLE='widows: 0; orphans: 0;padding-top:12px;'><FONT COLOR='#5162a5'><FONT FACE='Arial, sans-serif'><FONT SIZE=2><SPAN LANG='en-GB'><B>Name and address of <BR> organisation:</B></SPAN></FONT></FONT></FONT></P></TD>" + "<TD WIDTH=115 STYLE='border-top: 3.00pt solid #6773b6; border-bottom: 3.00pt solid #6773b6; border-left: 3.00pt solid #6773b6; border-right: none; padding-top: 0in; padding-bottom: 0in; padding-left: 0.08in; padding-right: 0in'>" + "<P CLASS='western' STYLE='widows: 0; orphans: 0;padding-top:12px;'><FONT COLOR='#5162a5'><FONT FACE='Arial, sans-serif'><FONT SIZE=2><SPAN LANG='en-GB'><B>Name and address of <BR> organisation:</B></SPAN></FONT></FONT></FONT></P></TD>" + "<TD WIDTH=115 STYLE='border-top: 3.00pt solid #6773b6; border-bottom: 3.00pt solid #6773b6; border-left: 3.00pt solid #6773b6; border-right: none; padding-top: 0in; padding-bottom: 0in; padding-left: 0.08in; padding-right: 0in'>" + "<P CLASS='western' STYLE='widows: 0; orphans: 0;padding-top:12px;'><FONT COLOR='#5162a5'><FONT FACE='Arial, sans-serif'><FONT SIZE=2><SPAN LANG='en-GB'><B>Name and address of <BR> organisation:</B></SPAN></FONT></FONT></FONT></P></TD>" + "<TD WIDTH=266 STYLE=border: 3.00pt solid #6773b6; padding-top: 0.08in; padding-bottom: 0.08in; padding-left: 0.08in; padding-right: 0.39in'><P CLASS='western' STYLE='margin-bottom: 0in; widows: 0; orphans: 0;padding-top:6px;'>" + "<FONT COLOR=\"#5162a5\"><FONT FACE=\"Arial, sans-serif\"><FONT SIZE=2><SPAN LANG=\"en-GB\"><B>Brief description of your work:</B></SPAN></FONT></FONT></FONT></P><P LANG=\"en-GB\" STYLE=\"margin-right: -0.34in; widows: 0; orphans: 0\">" + "<BR></P></TD></TR>"; using (StreamReader sr1 = new StreamReader(file)) { htmlString = sr1.ReadToEnd(); } htmlString = htmlString.Replace("@practicehours@", html); string baseUrl = System.Web.HttpContext.Current.Server.MapPath("~/"); string pdf_page_size = "A4"; 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; int webPageHeight = 0; // 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 = webPageWidth; converter.Options.WebPageHeight = webPageHeight; // create a new pdf document converting an url SelectPdf.PdfDocument doc = converter.ConvertHtmlString(htmlString, baseUrl); // save pdf document //doc.Save("Vehicle.pdf"); doc.Save(baseUrl + "Sample.pdf"); // close pdf document doc.Close(); }
public ActionResult getPDF() { // read parameters from the webpage string htmlString = @"<html><body><h1>Pakistan</h1></body></html>"; string baseUrl = ""; string pdf_page_size = "A4"; 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 = Convert.ToInt32("1024"); } catch { } int webPageHeight = 0; try { webPageHeight = Convert.ToInt32("1024"); } catch { } // 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 = webPageWidth; converter.Options.WebPageHeight = webPageHeight; // create a new pdf document converting an url PdfDocument doc = converter.ConvertHtmlString(htmlString, baseUrl); // 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) { // read parameters from the webpage string htmlString = collection["TxtHtmlCode"]; string baseUrl = collection["TxtBaseUrl"]; string pdf_page_size = collection["DdlPageSize"]; PdfPageSize pageSize = (PdfPageSize)Enum.Parse(typeof(PdfPageSize), pdf_page_size, true); string pdf_orientation = collection["DdlPageOrientation"]; PdfPageOrientation pdfOrientation = (PdfPageOrientation)Enum.Parse(typeof(PdfPageOrientation), pdf_orientation, true); int webPageWidth = 1024; try { webPageWidth = Convert.ToInt32(collection["TxtWidth"]); } catch { } int webPageHeight = 0; try { webPageHeight = Convert.ToInt32(collection["TxtHeight"]); } catch { } // 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 = webPageWidth; converter.Options.WebPageHeight = webPageHeight; // create a new pdf document converting an url PdfDocument doc = converter.ConvertHtmlString(htmlString, baseUrl); // 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 VoteProofDownload(string candidateId) { // read parameters from the webpage string htmlString = GetProofPdfHtml(candidateId); string baseUrl = ""; string pdf_page_size = PdfPageSize.A4.ToString(); PdfPageSize pageSize = (PdfPageSize)Enum.Parse(typeof(PdfPageSize), pdf_page_size, true); string pdf_orientation = PdfPageOrientation.Portrait.ToString(); PdfPageOrientation pdfOrientation = (PdfPageOrientation)Enum.Parse(typeof(PdfPageOrientation), pdf_orientation, true); int webPageWidth = 1024; try { webPageWidth = Convert.ToInt32(""); } catch { } int webPageHeight = 0; try { webPageHeight = Convert.ToInt32(""); } catch { } // 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 = webPageWidth; converter.Options.WebPageHeight = webPageHeight; // create a new pdf document converting an url PdfDocument doc = converter.ConvertHtmlString(htmlString, baseUrl); // 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 = "Vote-Proof-Download.pdf"; return(fileResult); }
public ActionResult ExportToPdf(string pYear, string pMonth, string pCode) { // read parameters from the webpage string htmlString = "<html><body><div style=\" margin-top:30px; margin-left:20px;\"><p>" + pMonth + " | " + pYear + "</p><p><b><h2>Código de Inspección: " + pCode + "</h2></b></p><div></body></html>"; string baseUrl = string.Empty; string pdf_page_size = "A4"; 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(900); } catch { } int webPageHeight = 0; try { webPageHeight = System.Convert.ToInt32(500); } catch { } // 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 = webPageWidth; converter.Options.WebPageHeight = webPageHeight; // create a new pdf document converting an url PdfDocument doc = converter.ConvertHtmlString(htmlString, baseUrl); // 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 = "Codigo_Inspeccion.pdf"; return(fileResult); }
public ActionResult GetPdf(string html) { // read parameters from the webpage string htmlString = html; string baseUrl = ""; 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 = Convert.ToInt32(TxtWidth.Text); } catch { } int webPageHeight = 0; try { //webPageHeight = Convert.ToInt32(TxtHeight.Text); } catch { } // 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 = webPageWidth; converter.Options.WebPageHeight = webPageHeight; // create a new pdf document converting an url PdfDocument doc = converter.ConvertHtmlString(htmlString, baseUrl); // save pdf document // doc.Save( false, "Sample.pdf"); doc.Save(Server.MapPath("~/" + "Sample" + ".pdf")); // close pdf document doc.Close(); return(Content("Documento Guardado!!")); }
public IActionResult OnPost() { // read parameters from the webpage PdfPageSize pageSize = (PdfPageSize)Enum.Parse(typeof(PdfPageSize), DdlPageSize, true); PdfPageOrientation pdfOrientation = (PdfPageOrientation)Enum.Parse(typeof(PdfPageOrientation), DdlPageOrientation, true); int webPageWidth = 1024; try { webPageWidth = Convert.ToInt32(TxtWidth); } catch { } int webPageHeight = 0; try { webPageHeight = Convert.ToInt32(TxtHeight); } catch { } // 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 = webPageWidth; converter.Options.WebPageHeight = webPageHeight; // create a new pdf document converting an url PdfDocument doc = converter.ConvertHtmlString(TxtHtmlCode, TxtBaseUrl); // 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); }
protected void BtnCreatePdf_Click(object sender, EventArgs e) { // read parameters from the webpage string htmlString = TxtHtmlCode.Text; string baseUrl = TxtBaseUrl.Text; string pdf_page_size = DdlPageSize.SelectedValue; PdfPageSize pageSize = (PdfPageSize)Enum.Parse(typeof(PdfPageSize), pdf_page_size, true); string pdf_orientation = DdlPageOrientation.SelectedValue; PdfPageOrientation pdfOrientation = (PdfPageOrientation)Enum.Parse(typeof(PdfPageOrientation), pdf_orientation, true); int webPageWidth = 1024; try { webPageWidth = Convert.ToInt32(TxtWidth.Text); } catch { } int webPageHeight = 0; try { webPageHeight = Convert.ToInt32(TxtHeight.Text); } catch { } // 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 = webPageWidth; converter.Options.WebPageHeight = webPageHeight; // create a new pdf document converting an url PdfDocument doc = converter.ConvertHtmlString(htmlString, baseUrl); // save pdf document doc.Save(Response, false, "Sample.pdf"); // close pdf document doc.Close(); }
public static Document GenerarHtmlaDocumento(string Url, PdfPageSize Tamaño) { PdfConverter pdfConvertidor = new PdfConverter(); PDFPageOrientation pdfPageOrientation; pdfConvertidor.LicenseKey = "UXpjcWBkcWlmcWd/YXFiYH9gY39oaGho"; pdfPageOrientation = PDFPageOrientation.Portrait; pdfConvertidor.PdfDocumentOptions.PdfPageSize = Tamaño; pdfConvertidor.PdfDocumentOptions.PdfCompressionLevel = PdfCompressionLevel.Normal; pdfConvertidor.PdfDocumentOptions.PdfPageOrientation = pdfPageOrientation; string thisPageURL = HttpContext.Current.Request.Url.AbsoluteUri; string baseUrl = thisPageURL; return(pdfConvertidor.GetPdfDocumentObjectFromHtmlString(Url, baseUrl)); }
public void ConvertPDF() { if (sesion == null) { sesion = SessionDB.start(Request, Response, false, db); } string htmlCode = @"<meta http-equiv='Content-Type' content='text/html; charset=UTF-8'/>"; htmlCode = HttpUtility.UrlDecode(sesion.vdata["html"], System.Text.Encoding.Default); // read parameters from the webpage string htmlString = htmlCode; string baseUrl = "";// collection["TxtBaseUrl"]; string pdf_page_size = "A4"; 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); // instantiate a html to pdf converter object HtmlToPdf converter = new HtmlToPdf(); // license key GlobalProperties.LicenseKey = "KgEbChgfGwoZGx0fChsSBBoKGRsEGxgEExMTEw=="; // set converter options converter.Options.PdfPageSize = pageSize; converter.Options.PdfPageOrientation = pdfOrientation; converter.Options.WebPageWidth = Convert.ToInt32(1024); converter.Options.WebPageHeight = Convert.ToInt32(0); converter.Options.MarginTop = 35; converter.Options.MarginBottom = 35; // create a new pdf document converting an url PdfDocument doc = converter.ConvertHtmlString(htmlString, baseUrl); // save pdf document byte[] pdf = doc.Save(); Response.ContentType = "application/pdf"; Response.AddHeader("content-disposition", "inline; filename=ConstanciaRetencion.pdf"); Response.BinaryWrite(pdf); // close pdf document doc.Close(); }
public PdfDocument() { m_Fonts = new List <PdfFont>(); m_Meta = new PdfMeta(); m_Info = new PdfInfo(); m_OutLines = new PdfOutlines(); m_Root = new PdfRoot(); m_PageTree = new PdfPageTree(); m_Trailer = new PdfTrailer(); m_ObjectRepository = new ObjectRepository(); m_ResourceRepository = new ResourceRepository(); m_Root.Info = m_Info; m_Root.Outlines = m_OutLines; m_Root.PageTree = m_PageTree; m_Trailer.Root = m_Root; m_PageSize = PdfPageSize.Default(); }
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 FileModel GetPdfFromHtmlString(string html, string fileName, PdfPageSize pageSize = PdfPageSize.A4, PdfPageOrientation orientation = PdfPageOrientation.Portrait) { var result = new FileModel() { FileName = fileName, Mime = MimeMapping.GetMimeMapping(fileName) }; HttpClient client = new HttpClient(); var req = HttpContext.Current.Request; //string url = req.Url.Scheme + "://" + req.Url.Authority + req.ApplicationPath.TrimEnd('/') + actionUrl; ////string url = actionUrl; //File.WriteAllText("C:\\Log\\pdf_serive" + DateTime.Now.ToString("yyyyMMddhhmmss") + ".txt", url); HtmlToPdf converter = new HtmlToPdf(); // set converter options converter.Options.PdfPageSize = pageSize; converter.Options.PdfPageOrientation = orientation; converter.Options.MaxPageLoadTime = 200; converter.Options.MarginTop = 5; System.Drawing.Font font = new System.Drawing.Font("Arial", 1); //PdfTextSection footer = new PdfTextSection(0, 10, "Dokumen ini adalah surat perintah bayar yang sah walaupun tanpa tanda tangan pejabat ybs, dicetak dari system User Purchase PDSI", font); //converter.Footer.Add(footer); // create a new pdf document converting an url PdfDocument doc = converter.ConvertHtmlString(html); // save pdf document var byteresult = doc.Save(); result.Bytes = byteresult; // close pdf document doc.Close(); return(result); }
public void ConvertToPdf() { string url = Url; string pdf_page_size = "A4"; 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; int webPageHeight = 0; // 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 = webPageWidth; converter.Options.WebPageHeight = webPageHeight; SetBusy(true, "Retrieving PDF..."); PdfDocument doc = null; Task.Factory.StartNew(() => { // create a new pdf document converting an url doc = converter.ConvertUrl(url); }).ContinueWith((t) => { // save pdf document doc.Save("Sample.pdf"); // close pdf document doc.Close(); FilePath = "Sample.pdf"; FilePath = Path.GetFullPath(FilePath); SetBusy(false, "Done!"); }); }
public PdfDocument Html2PdfDoc(string html, PdfPageSize size) { try { var document = new PdfDocument { SerialNumber = SERIAL_NUMBER, Security = { AllowEditContent = false, AllowCopyContent = false } }; var page = document.AddPage(size, new PdfDocumentMargins(0, 0, 0, 0), PdfPageOrientation.Landscape); page.DisplayFooter = false; page.DisplayHeader = false; //var page = document.AddPage(new PdfPageSize(640,455), new PdfDocumentMargins(4,4, 4,4)); var pageHtml = new PdfHtml(html, Utils.BaseUrl()); page.Layout(pageHtml); return(document); } catch (Exception) { return(null); } }
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); }
//Genera el arreglo de bits para el base64 del PDF public byte[] GetPDF(string pHTML) { byte[] bPDF = null; MemoryStream ms = new MemoryStream(); HtmlToPdf converter = new HtmlToPdf(); string pdf_page_size = "Letter"; PdfPageSize pageSize = (PdfPageSize)Enum.Parse(typeof(PdfPageSize), pdf_page_size, true); // page settings converter.Options.PdfPageSize = pageSize; converter.Options.WebPageWidth = 1024; converter.Options.WebPageHeight = 0; converter.Header.Height = 50; converter.Options.MarginLeft = 15; converter.Options.MarginRight = 15; converter.Options.MarginTop = 30; converter.Options.MarginBottom = 30; //// header settings //converter.Options.DisplayHeader = true; //converter.Header.DisplayOnFirstPage = true; //converter.Header.DisplayOnOddPages = false; //converter.Header.DisplayOnEvenPages = false; //converter.Header.Height = 90; //// footer settings //converter.Options.DisplayFooter = true; //converter.Footer.DisplayOnFirstPage = true; //converter.Footer.DisplayOnOddPages = false; //converter.Footer.DisplayOnEvenPages = false; //converter.Footer.Height = 60; var Texto_FormatHTML = pHTML; PdfDocument doc = converter.ConvertHtmlString(Texto_FormatHTML); doc.Save(ms); doc.Close(); bPDF = ms.ToArray(); return(bPDF); }
public IHttpActionResult ConvertHTML_PDF(String code_html) { try { string pagezise = "A4"; string orientation = "Portrait"; int width = 1024; int heigtn = 100; ApiHTMLtoPDF.Layout.PDF pdf = new Layout.PDF(); pdf._CodeHTML = code_html; pdf._PageZise = pagezise; pdf._Orientation = orientation; pdf._Width = width; pdf._Height = heigtn; PdfPageSize pagZise = (PdfPageSize)Enum.Parse(typeof(PdfPageSize), pagezise, true); PdfPageOrientation Orientation = (PdfPageOrientation)Enum.Parse(typeof(PdfPageOrientation), orientation, true); int webPageWidth = Convert.ToInt32(width); int webPageHeight = Convert.ToInt32(heigtn); HtmlToPdf converter = new HtmlToPdf(); converter.Options.PdfPageSize = pagZise; converter.Options.PdfPageOrientation = Orientation; converter.Options.WebPageWidth = webPageWidth; converter.Options.WebPageHeight = webPageHeight; PdfDocument doc = converter.ConvertHtmlString(code_html, ""); HttpResponse httpResponse = new HttpResponse(new StreamWriter(new MemoryStream())); doc.Save(httpResponse, false, "Sample.pdf"); doc.Close(); return(Ok()); } catch (Exception ex) { throw; } }
/// <summary> /// Sets the page size to PageSize.A4 for instance. /// </summary> /// <param name="size">selected page size</param> public void PageSize(PdfPageSize size) { _pdfReport.DataBuilder.DefaultPageSize(size.ToRectangle()); }
public byte[] GenerarPDF_Horizontal(string viewName, object model, ControllerContext ctx, PdfPageSize pageSize, PdfPageOrientation orientation, float bottom, float top, float left, float right, bool hasFooter = true, bool hasHeader = false) { String View = RenderRazorViewToString(viewName, model, ctx); HiQPdf.HtmlToPdf docpdf = GetHtmlToPdf(bottom, top, left, right); docpdf.Document.PageOrientation = PdfPageOrientation.Landscape; SetFooter(docpdf.Document); if (hasFooter) SetFooter(docpdf.Document); if (hasHeader) SetHeader(docpdf.Document); byte[] archivo = docpdf.ConvertHtmlToPdfDocument(View, "").WriteToMemory(); return archivo; }