private void ExportDivContentToPDF() { System.Web.HttpResponse Response = System.Web.HttpContext.Current.Response; try { // create an API client instance string userName = ConfigurationManager.AppSettings["pdfcrowdUsername"].ToString(); string APIKey = ConfigurationManager.AppSettings["pdfcrowdAPIKey"].ToString(); pdfcrowd.Client client = new pdfcrowd.Client(userName, APIKey); // convert a web page and write the generated PDF to a memory stream MemoryStream Stream = new MemoryStream(); //client.convertURI("http://www.google.com", Stream); // set HTTP response headers Response.Clear(); Response.AddHeader("Content-Type", "application/pdf"); Response.AddHeader("Cache-Control", "max-age=0"); Response.AddHeader("Accept-Ranges", "none"); Response.AddHeader("Content-Disposition", "attachment; filename=TigerReservePdfExport.pdf"); System.IO.StringWriter stringWrite1 = new System.IO.StringWriter(); System.Web.UI.HtmlTextWriter htmlWrite1 = new HtmlTextWriter(stringWrite1); MainContent.RenderControl(htmlWrite1); client.convertHtml(stringWrite1.ToString(), Stream); // send the generated PDF Stream.WriteTo(Response.OutputStream); Stream.Close(); Response.Flush(); Response.End(); } catch (pdfcrowd.Error why) { Response.Write(why.ToString()); } }
public void Convert(string htmlContent, string filename) { // Create a client instance if needed. Take credentials from Registry if (client == null) { RegistryKey key = Registry.LocalMachine.OpenSubKey(@"Software\Wow6432Node\Opentext\Capture Center\ScriptingUtilities\PdfCrowd"); string username = (string)key.GetValue("user"); string api_key = (string)key.GetValue("api_key"); client = new pdfcrowd.Client(username, api_key); } // Convert HTML string and store result in file MemoryStream memStream = new MemoryStream(); client.convertHtml(htmlContent, memStream); using (FileStream file = new FileStream(filename, FileMode.Create, System.IO.FileAccess.Write)) { byte[] bytes = new byte[memStream.Length]; memStream.Read(bytes, 0, (int)memStream.Length); file.Write(bytes, 0, bytes.Length); memStream.Close(); } // Log remaining tokens int ntokens = client.numTokens(); trace.WriteInfo("PdfCrowd remaining ´tokens: " + ntokens); }
public async Task <IActionResult> PdfCrowd() { string htmlContent = this.view.Render("Home/PdfCrowd"); HttpResponse Response = HttpContext.Response; pdfcrowd.Client client = new pdfcrowd.Client("surendrakandira", "06277a91ab88bd95efab907bcdbd9bf5"); MemoryStream Stream = new MemoryStream(); await client.convertHtml(htmlContent, Stream); string fileName = string.Format("{0}.pdf", DateTime.Now.ToString("ddMMyyyyhhmmss")); // set HTTP response headers byte[] byteInfo = Stream.ToArray(); ////Stream.Write(byteInfo, 0, byteInfo.Length); ////Stream.Position = 0; ////return this.File(Stream, "application/pdf", fileName); //// HttpContext.Response.ContentType = "application/pdf"; ///HttpContext.Response.Body.Write(byteInfo, 0, byteInfo.Length); return(this.File(byteInfo, "application/pdf")); }
/// <summary> /// Overriding Render to intercept html when page_load is ready so we can use it for HTML to PDF conversion /// </summary> /// <param name="writer">HtmlTextWriter</param> protected override void Render(HtmlTextWriter writer) { var sbOut = new StringBuilder(); var swOut = new StringWriter(sbOut); var htwOut = new HtmlTextWriter(swOut); base.Render(htwOut); string sOut = sbOut.ToString(); var isDetailed = (bool)Session["IsDetailedTest"]; var isPdfPrint = (bool)Session["IsPdfPrint"]; string year = System.DateTime.Today.Year.ToString(); int month = System.DateTime.Today.Month; int day = System.DateTime.Today.Day; int hour = System.DateTime.Now.Hour; int minute = System.DateTime.Now.Minute; string date = year + "-" + AddZero(month) + "-" + AddZero(day) + "_" + AddZero(hour) + "." + AddZero(minute); // URL format will be: http.example.com string url = Session["MainUrl"].ToString().Replace("://", ".").Replace("/", "-"); // Filename with format -> YY-MM-DD_hh.mm Rapportage_http.example.com string filename = @"" + date + " Rapportage_" + url; if (!isDetailed) filename = filename + "_basis"; if (isPdfPrint) { try { // create an API client instance //Currently using my personal free license account var ApiKey = System.Web.Configuration.WebConfigurationManager.AppSettings["PdfCrowd"]; pdfcrowd.Client client = new pdfcrowd.Client("Peacefield", ApiKey); client.setHtmlZoom(100); // convert a web page and write the generated PDF to a memory stream MemoryStream Stream = new MemoryStream(); client.convertHtml(sOut, Stream); // set HTTP response headers Response.Clear(); Response.AddHeader("Content-Type", "application/pdf"); Response.AddHeader("Cache-Control", "max-age=0"); Response.AddHeader("Accept-Ranges", "none"); Response.AddHeader("Content-Disposition", "attachment; filename=" + filename + ".pdf"); // send the generated PDF Stream.WriteTo(Response.OutputStream); Stream.Close(); Response.Flush(); Response.End(); } catch (pdfcrowd.Error why) { Response.Write(why.ToString()); } } else { // Write the string to a file with format -> Rapportage_http.example.com_YY-MM-DD--hh.mm.ss //System.IO.StreamWriter file = new System.IO.StreamWriter(@"c:\users\michael\dropbox\hw\Stageopdracht_Dotsolutions\_TestUitrollingen\" // + filename + ".html"); //file.WriteLine(sOut); //file.Close(); System.IO.StreamWriter file = new System.IO.StreamWriter(Server.MapPath("~/Temp/" + filename + ".html")); file.WriteLine(sOut); file.Close(); try { Response.ContentType = "application/octet-stream"; Response.AppendHeader("content-disposition", "attachment;filename=" + filename + ".html"); Response.TransmitFile(Server.MapPath("~/Temp/" + filename + ".html")); Response.Flush(); } finally { string fullpath = Server.MapPath("~/Temp/" + filename + ".html"); if (System.IO.File.Exists(fullpath)) { Debug.WriteLine("File exists"); Debug.WriteLine("fullpath" + fullpath); System.IO.File.Delete(fullpath); Debug.WriteLine("File deleted"); } else { Debug.WriteLine("File does not exist"); } } Response.End(); } writer.Write(sOut); }