static void Main(string[] args) { Console.WriteLine("Hello World!"); var a2pClient = new Api2Pdf("YOUR-API-KEY"); //portal.api2pdf.com/register var result = a2pClient.HeadlessChrome.FromHtml("<p>Hello World</p>"); Console.WriteLine(result.Pdf); }
public IActionResult Pdf(int id) { string url = this.Url.Action("ViewOrderPdf", "External", new { id }, "https"); HttpClient httpClient = new HttpClient(); string html = httpClient.GetAsync(url).Result.Content.ReadAsStringAsync().Result; var apiToPdfClient = new Api2Pdf(""); var options = new Dictionary <string, string> { { "landscape", "true" } }; var apiResponse = apiToPdfClient.HeadlessChrome.FromHtml(html, inline: true, outputFileName: "Order" + id + ".pdf", options: options); return(Redirect(apiResponse.Pdf)); }
public ActionResult Handle(PdfModel model) { //Use NUGET //install-package api2pdf var a2pClient = new Api2Pdf("YOURAPIKEY"); var pdfUrl = a2pClient.WkHtmlToPdf.FromHtml(model.Content).Pdf; var result = new { pdfUrl = pdfUrl }; return(Content(JsonConvert.SerializeObject(result), "application/json")); }
public IActionResult PaymentSheet(DateTime fromDate) { HtmlToPdf converter = new HtmlToPdf(); converter.Options.PdfPageOrientation = PdfPageOrientation.Landscape; string url = this.Url.Action("ViewPaymentSheetPdf", "External", new { fromDate }, "https"); HttpClient httpClient = new HttpClient(); string html = httpClient.GetAsync(url).Result.Content.ReadAsStringAsync().Result; var apiToPdfClient = new Api2Pdf(""); var apiResponse = apiToPdfClient.HeadlessChrome.FromHtml(html, inline: true, outputFileName: "PaymentSheet" + fromDate.ToString("dd/MM/yyyy") + ".pdf"); return(Redirect(apiResponse.Pdf)); }
public IActionResult Handle([FromBody] PdfModel model) { //Use NUGET //install-package api2pdf var a2pClient = new Api2Pdf("YOURAPIKEY"); var pdfUrl = a2pClient.WkHtmlToPdf.FromHtml(model.Content).Pdf; var result = new { pdfUrl = pdfUrl }; //Uncomment and modify if CORS is required //this.HttpContext.Response.Headers.Add("Access-Control-Allow-Origin", "*"); return(Content(JsonConvert.SerializeObject(result), "application/json")); }
public void ProcessRequest(HttpContext context) { using (var sr = new System.IO.StreamReader(context.Request.InputStream)) { var data = sr.ReadToEnd(); var model = JsonConvert.DeserializeObject <PdfModel>(data); context.Response.ContentType = "application/json"; var a2pClient = new Api2Pdf("YOURAPIKEY"); var pdfUrl = a2pClient.WkHtmlToPdf.FromHtml(model.Content).Pdf; var result = new { pdfUrl = pdfUrl }; context.Response.Write(JsonConvert.SerializeObject(result)); } }
public void ProcessRequest(HttpContext context) { using (var sr = new System.IO.StreamReader(context.Request.InputStream)) { var data = sr.ReadToEnd(); var model = JsonConvert.DeserializeObject <PdfModel>(data); context.Response.ContentType = "application/json"; var a2pClient = new Api2Pdf("72e2fa4b-8d2a-425f-a017-02d33cc97f27"); var pdfUrl = a2pClient.WkHtmlToPdf.FromHtml(model.Content).Pdf; string taskid = HttpContext.Current.Session["Name"].ToString(); HttpContext.Current.Session.Timeout = 60; string savedFileName = System.Web.HttpContext.Current.Server.MapPath("~\\storepdf\\" + taskid + ".pdf"); var client = new WebClient(); client.DownloadFile(pdfUrl, savedFileName); var result = new { pdfUrl = pdfUrl }; context.Response.Write(JsonConvert.SerializeObject(result)); //addhaederfooter(); AddDataTableToPDF(); updatereleasepdf(); } }
static void Main(string[] args) { if (string.IsNullOrEmpty(API_KEY)) { Console.WriteLine("Set API Key variable before running script"); return; } Console.WriteLine("Starting up..."); var a2pClient = new Api2Pdf(API_KEY); //test data string sampleHtml = "<p>Hello World</p>"; string sampleWebsite = "https://www.api2pdf.com"; string sampleDocxFile = "https://www.api2pdf.com/wp-content/themes/api2pdf/assets/samples/sample-word-doc.docx"; string sampleHtmlFile = "http://www.api2pdf.com/wp-content/uploads/2021/01/sampleHtml.html"; string sampleHtmlToXlsxFile = "http://www.api2pdf.com/wp-content/uploads/2021/01/sampleTables.html"; string samplePdf = "http://www.api2pdf.com/wp-content/uploads/2021/01/1a082b03-2bd6-4703-989d-0443a88e3b0f-4.pdf"; string sampleLongPdf = "http://www.api2pdf.com/wp-content/uploads/2022/03/7b66d080-3120-477a-a213-87938b1b0c08.pdf"; Api2PdfResult result; //Chrome - HTML to PDF result = a2pClient.Chrome.HtmlToPdf(new ChromeHtmlToPdfRequest { Html = sampleHtml }); Console.WriteLine($"Chrome HTML to PDF: {result.FileUrl}"); //Chrome - HTML to PDF result = a2pClient.Chrome.HtmlToPdf(new ChromeHtmlToPdfRequest { Html = sampleHtml, FileName = "test.pdf", Inline = false, Options = new ChromeHtmlToPdfOptions { Delay = 3000, HeaderTemplate = "<div class=\"page-footer\" style=\"width:100%; text-align:right; font-size:12px;\">Page <span class=\"pageNumber\"></span> of <span class=\"totalPages\"></span></div>", FooterTemplate = "<div class=\"page-footer\" style=\"width:100%; text-align:right; font-size:12px;\">Page <span class=\"pageNumber\"></span> of <span class=\"totalPages\"></span></div>", DisplayHeaderFooter = true, Landscape = true } }); Console.WriteLine($"Chrome HTML to PDF, additional options: {result.FileUrl}"); //Chrome - URL to PDF result = a2pClient.Chrome.UrlToPdf(new ChromeUrlToPdfRequest { Url = sampleWebsite }); Console.WriteLine($"Chrome URL to PDF: {result.FileUrl}"); //Chrome - HTML to Image result = a2pClient.Chrome.HtmlToImage(new ChromeHtmlToImageRequest { Html = sampleHtml }); Console.WriteLine($"Chrome HTML to Image: {result.FileUrl}"); //Chrome - URL to Image result = a2pClient.Chrome.UrlToImage(new ChromeUrlToImageRequest { Url = "https://www.api2pdf.com" }); Console.WriteLine($"Chrome Url to Image: {result.FileUrl}"); //Wkhtml //Wkhtml - HTML to PDF result = a2pClient.Wkhtml.HtmlToPdf(new WkhtmlHtmlToPdfRequest { Html = sampleHtml }); Console.WriteLine($"Wkhtml HTML to PDF: {result.FileUrl}"); //Wkhtml - HTML to PDF result = a2pClient.Wkhtml.HtmlToPdf(new WkhtmlHtmlToPdfRequest { Html = sampleHtml, FileName = "sample.pdf", Inline = false, EnableToc = true, Options = new System.Collections.Generic.Dictionary <string, string>() { { "orientation", "landscape" } } }); Console.WriteLine($"Wkhtml HTML to PDF, additional options and Table of Contents enabled: {result.FileUrl}"); //Wkhtml - URL to PDF result = a2pClient.Wkhtml.UrlToPdf(new WkhtmlUrlToPdfRequest { Url = sampleWebsite }); Console.WriteLine($"Wkhtml URL to PDF: {result.FileUrl}"); //libreoffice //LibreOffice - Any to PDF result = a2pClient.LibreOffice.AnyToPdf(new LibreFileConversionRequest { Url = sampleDocxFile }); Console.WriteLine($"LibreOffice Docx to PDF: {result.FileUrl}"); //LibreOffice - Html to Docx result = a2pClient.LibreOffice.HtmlToDocx(new LibreFileConversionRequest { Url = sampleHtmlFile }); Console.WriteLine($"LibreOffice Html to Docx: {result.FileUrl}"); //LibreOffice - Html to Xlsx result = a2pClient.LibreOffice.HtmlToXlsx(new LibreFileConversionRequest { Url = sampleHtmlToXlsxFile }); Console.WriteLine($"LibreOffice Html to Xlsx: {result.FileUrl}"); //LibreOffice - PDF to HTML result = a2pClient.LibreOffice.PdfToHtml(new LibreFileConversionRequest { Url = samplePdf }); Console.WriteLine($"LibreOffice PDF to HTML: {result.FileUrl}"); //PdfSharp //PdfSharp - Merge PDFs together result = a2pClient.PdfSharp.MergePdfs(new PdfMergeRequest { Urls = new List <string>() { samplePdf, samplePdf, samplePdf } }); Console.WriteLine($"Merge PDFs: {result.FileUrl}"); //PdfSharp - add bookmarks result = a2pClient.PdfSharp.SetBookmarks(new PdfBookmarkRequest { Url = result.FileUrl, Bookmarks = new List <PdfBookmark>() { new PdfBookmark { Page = 0, Title = "Introduction" }, new PdfBookmark { Page = 1, Title = "Second Page" } } }); Console.WriteLine($"PdfSharp - set bookmarks: {result.FileUrl}"); //PdfSharp - add password result = a2pClient.PdfSharp.SetPassword(new PdfPasswordRequest { Url = samplePdf, UserPassword = "******" }); Console.WriteLine($"PdfSharp - set password: {result.FileUrl}"); //PdfSharp - extract pages result = a2pClient.PdfSharp.ExtractPages(new PdfExtractPagesRequest { Url = sampleLongPdf, Start = 0, End = 2 }); Console.WriteLine($"PdfSharp - extract pages: {result.FileUrl}"); //Delete PDF result = a2pClient.Chrome.HtmlToPdf(new ChromeHtmlToPdfRequest { Html = sampleHtml }); a2pClient.Utilities.Delete(result.ResponseId); Console.WriteLine($"Delete PDF: this url should break: {result.FileUrl}"); }