I looking for the easiest way to export the currently viewed asp.net web page to a PDF document using iTextSharp - it can be either a screenshot of it or passing in the url to generate the document. Sample code would be greatly appreciated. Many thanks in advance!
On a past project, we used Supergoo ABCPDF
to do something like you need to and it worked quite well. You basicly feed it an url and it process the HTML page into a PDF.
On the downside, it's a licensed software with costs associated to it and we had some performance issues when exporting a lot of large PDF at the same time.
Hope this helps !
Adding to what Darin said in his comment.
You can try using wkhtmltopdf to generate PDF files. It takes URL as input. This is what I have used for my SO application so2pdf.
It is not that easy (or i think so), i had same problem and i had to write code to generate exact page in pdf. It depends of page and used styles etc. So i create drawing of each element.
For some project i used Winnovative HTML to PDF converter but it is not free.
Give a try with PDFSharp (to generate PDF files at runtime), and it's Open Source library :
http://pdfsharp.com/PDFsharp/index.php?option=com_content&task=view&id=27&Itemid=1
Alternative solution : http://www.bratched.com/en/component/content/article/76-how-to-convert-xhtml-to-pdf-in-c.html
There is an example Convert the Current HTML Page to PDF on WInnovative website which does exactly this. The relevant C# code to convert the currently viewed asp.net web page to a PDF document is:
// Controls if the current HTML page will be rendered to PDF or as a normal page
bool convertToPdf = false;
protected void convertToPdfButton_Click(object sender, EventArgs e)
{
// The current ASP.NET page will be rendered to PDF when its Render method will be called by framework
convertToPdf = true;
}
protected override void Render(HtmlTextWriter writer)
{
if (convertToPdf)
{
// Get the current page HTML string by rendering into a TextWriter object
TextWriter outTextWriter = new StringWriter();
HtmlTextWriter outHtmlTextWriter = new HtmlTextWriter(outTextWriter);
base.Render(outHtmlTextWriter);
// Obtain the current page HTML string
string currentPageHtmlString = outTextWriter.ToString();
// Create a HTML to PDF converter object with default settings
HtmlToPdfConverter htmlToPdfConverter = new HtmlToPdfConverter();
// Set license key received after purchase to use the converter in licensed mode
// Leave it not set to use the converter in demo mode
htmlToPdfConverter.LicenseKey = "fvDh8eDx4fHg4P/h8eLg/+Dj/+jo6Og=";
// Use the current page URL as base URL
string baseUrl = HttpContext.Current.Request.Url.AbsoluteUri;
// Convert the current page HTML string a PDF document in a memory buffer
byte[] outPdfBuffer = htmlToPdfConverter.ConvertHtml(currentPageHtmlString, baseUrl);
// Send the PDF as response to browser
// Set response content type
Response.AddHeader("Content-Type", "application/pdf");
// Instruct the browser to open the PDF file as an attachment or inline
Response.AddHeader("Content-Disposition", String.Format("attachment; filename=Convert_Current_Page.pdf; size={0}", outPdfBuffer.Length.ToString()));
// Write the PDF document buffer to HTTP response
Response.BinaryWrite(outPdfBuffer);
// End the HTTP response and stop the current page processing
Response.End();
}
else
{
base.Render(writer);
}
}
Related
I have tried multiple plugins and c# classes to try and convert the HTML and CSS on my asp.net project to a pdf and even though the code used looks fine, and the button click works for other functions, I just cannot seem to get any html to pdf function to work. Has anyone else encountered this, or know if there is something I have missed to resolve it?
This is the latest code I have tried for hiqpdf in C#:
protected void Print_Button_Click(object sender, EventArgs e)
{
HtmlToPdf htmlToPdfConverter = new HtmlToPdf();
// set PDF page size, orientation and margins
htmlToPdfConverter.Document.PageSize = PdfPageSize.A4;
htmlToPdfConverter.Document.PageOrientation = PdfPageOrientation.Portrait;
htmlToPdfConverter.Document.Margins = new PdfMargins(0);
// convert HTML to PDF
htmlToPdfConverter.ConvertUrlToFile("http://localhost:51091/Printout","mcn.pdf");
}
It is not stated directly within theHiQpdf documentation of the method, but the method ConvertUrlToFile() stores the produced pdf file locally on the disc. On some example page (Convert URLs and HTML Code to PDF) the following comment can be found:
// ConvertUrlToFile() is called to convert the html document and save the resulted PDF into a file on disk
// Alternatively, ConvertUrlToMemory() can be called to save the resulted PDF in a buffer in memory
htmlToPdfConverter.ConvertUrlToFile(url, pdfFile);
Since your example shows a button-click eventhandler, the file is probably generated but not used to return in the http response. You have to write the data into the response. The methods ConvertToStream() or ConvertToMemory should come in handy to do so. Don't forget to use Response.Clear() or Response.ClearContent() and Response.ClearHeader() before that and Flush() and Close() afterwards.
I want to convert an html string to pdf with images located at a specific location. I have the html code loaded into a string. I have the images that go with the html located in c:\temp\ . I can get this to work if I save the html string to a file as seen in the example below. I really dont need to write the html to a file. Does anyone have any ideas? I'm using the 3rd party component HiQPDF and I have already contacted them.
private void ConvertToPDF(string htmlbody, string pdfname)
{
HtmlToPdf htmlToPdfConverter = new HtmlToPdf();
// set a demo serial number
htmlToPdfConverter.SerialNumber = "-- HiQPdf Serial Number --";
htmlToPdfConverter.Document.Margins = new PdfMargins(5);
//doesnt work
htmlToPdfConverter.ConvertHtmlToFile(htmlbody, #"c:\temp\", pdfname);
//works just fine :-( , but it forces me to save to the disk
htmlToPdfConverter.ConvertUrlToFile(#"C:\temp\1Z7039680342477761-00a26b62-f6d3-47e3-92c3-18b907665aaa.html", pdfname);
return;
}
Set the base URL as file:///C:\temp/ instead of "c:\temp". This information comes directly from hiqpdf support.
I have requirement to save an XML document which is displayed in a HTML page.
The scenario is like this: I am sending search request to the server and in return I am getting xml file but displayed in html page. What I want is to save client-side the xml file displayed inside the html form, using javascript, asp.net (C#).
Please see this link (server return file like this)
http://www.w3schools.com/dom/books.xml
You need to save the stream from your request into a xmldoc.
Should be really straight forward.
Have a look here:
http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.save(v=vs.100).aspx
Also, here there is a microsoft article about it:
http://support.microsoft.com/kb/307643
The sent file should have a HTTP header indicating it's meant to be saved, not shown.
Do it this way:
context.Response.ContentType = "application/force-download";
Assuming you are writing a client app to consume this file
using System.Net;
new WebClient().DownloadFile("http://www.w3schools.com/dom/books.xml", #"c:\books.xml");
As Rutesh said you can't go with javascript. Save it on the server with C#.NET and then access it from server. Secondly, the page you are talking about is not an html page its an xml file. I hope you know how to build an asp.net page, if yes then use this:
XmlDocument xdoc = new XmlDocument();
xdoc.Load("http://www.w3schools.com/dom/books.xml");
xdoc.Save("myfilename.xml");
Happy coding!
sourceResponse.ContentType = "application/octet-stream";
byte[] responseContent = "XML File content"
sourceResponse.AddHeader("content-disposition", String.Format("attachment; filename={0}.xml", "yourFileName"));
sourceResponse.BinaryWrite(responseContent);
sourceResponse.End();
In my Aspx page I have two buttons, btnGenerateReceipt is for generating receipt and btnAddNew for adding new receord. OnClick event of btnGenerateReceipt I am generating and opening a receipt as below.
protected void onGenerateReceipt(object sender, EventArgs e)
{
try
{
byte[] document = receiptByte;
Response.ClearContent();
Response.ClearHeaders();
Response.Buffer = true;
Response.ContentType = "application/vnd.ms-word";
Response.AddHeader("content-disposition", "inline;filename=" + "Receipt" + ".doc");
Response.Charset = "";
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.BinaryWrite(document);
//Response.Flush();
}
}
catch (Exception ex)
{
}
finally
{
//Response.End();
}
}
}
This opens Open/Save/Cancel dialog box, followings are my problems,
I need the word document to open automatically without the dialog box.
My second button's click function doesn't fire after I click btnGenerateReceipt button.
How can I generate&Open PDF file instead of word Doc?
Any idea?
The content sent by the server is handled by the web browser. You can not control from server side code whether the browser opens, saves or asks the user by default, as this is a browser setting.
EDIT
As for the second question about generating a PDF: There are many libraries out there to generate PDFs. However, if you already have the Word file ready, one solution would be to print the Word document to a PDF printer and send the resulting PDF.
Printing the document can be achieved using ShellExecute or the Process class with the verb print, then you could use a PDF printer like PDF-Creator or Bullzip to generate a PDF file.
This is what I'd try instead of "manually" generating the PDF file.
I need the word document to open automatically without the dialog box.
For the answer of the go with #Thorsten Dittmar answer.
My second button's click function doesn't fire after I click btnGenerateReceipt button.
Asp.net uses stateless connection, so do you think your written contents will remain in memory. i think it should not work as per my understanding. create response content and then write it to response and flush it.
How can I generate&Open PDF file instead of word Doc?
To generate pdf reference this. use iTextSharp like library to generate pdf then export/ save them as pdf.
Ref: ASP.NET 4: HttpResponse open in NEW Browser?
Response.AppendHeader("Content-Disposition", "inline; filename=foo.pdf");
You need to set the Content Type of the Response object and add the binary form of the pdf in the header. See this post for details:
Ref: Opening a PDF File from Asp.net page
private void ReadPdfFile()
{
string path = #"C:\Swift3D.pdf";
WebClient client = new WebClient();
Byte[] buffer = client.DownloadData(path);
if (buffer != null)
{
Response.ContentType = "application/pdf";
Response.AddHeader("content-length",buffer.Length.ToString());
Response.BinaryWrite(buffer);
}
}
Ref Links:
ASP.NET 4: HttpResponse open in NEW Browser?
Generate pdf file after retrieving the information
ASP.NET MVC: How can I get the browser to open and display a PDF instead of displaying a download prompt?
I'm sending emails that have invoices attached as PDFs. I'm already - elsewhere in the application - creating the invoices in an .aspx page. I'd like to use Server.Execute to return the output HTML and generate a PDF from that. Otherwise, I'd have to use a reporting tool to "draw" the invoice on a PDF. That blows for lots of reasons, not the least of which is that I'd have to update both the .aspx page and the report for every minor change. What to do...
There is no way to generate a PDF from an HTML string directly within .NET, but there are number of third party controls that work well.
I've had success with this one: http://www.html-to-pdf.net
and this: http://www.htmltopdfasp.net
The important questions to ask are:
Does it render correctly as compared to the 3 major browsers: IE, FF and Safari/Chrome?
Does it handle CSS fine?
Does the control have it's own rendering engine? If so, bounce it. You don't want to trust a home grown rendering engine - the browsers have a hard enough problem getting everything pixel perfect.
What dependencies does the third party control require? The fewer, the better.
There are a few others but they deal with ActiveX displays and such.
We use a product called ABCPDF for this and it works fantastic.
http://www.websupergoo.com/abcpdf-1.htm
This sounds like a job for Prince. It can take HTML and CSS and generate a PDF, which you can then present to your users. It supports CSS3 better than most web browsers (staff include HÃ¥kon Wium Lie, the inventor of CSS).
See the samples, especially the ones for Wikipedia pages, for the beautiful output it can generate. There's also an interesting Google Tech Talk with the authors.
Edit: There is a .NET wrapper available.
wkhtmltopdf is a free and cool exe to generate pdf from html. Its written in c++. But nReco htmltopdf is a wrapper dotnet library for this awesome tool. I implemented using this dotnet library and it was just so good it does everything by its own you just need to give html as a data source.
/// <summary>
/// Converts html into PDF using nReco dll and wkhtmltopdf.exe.
/// </summary>
private byte[] ConvertHtmlToPDF()
{
HtmlToPdfConverter nRecohtmltoPdfObj = new HtmlToPdfConverter();
nRecohtmltoPdfObj.Orientation = PageOrientation.Portrait;
nRecohtmltoPdfObj.PageFooterHtml = CreatePDFFooter();
nRecohtmltoPdfObj.CustomWkHtmlArgs = "--margin-top 35 --header-spacing 0 --margin-left 0 --margin-right 0";
return nRecohtmltoPdfObj.GeneratePdf(CreatePDFScript() + ShowHtml() + "</body></html>");
}
The above function is an excerpt from the below link post which explains it in detail.
HTML to PDF in ASP.Net
The initial question is about converting another aspx page containing an invoice to a PDF document. The invoice is probably using some session data and the user suggests to use Server.Execute() to obtain the invoice page HTML code and then to convert that code to PDF. Converting the invoice page URL directly is not possible because a new session would be created during conversion and the session data would be lost.
This is actually a good technique to preserve session data during conversion which is applied in Convert a HTML Page to PDF in Same Session ASP.NET Demo of the EvoPdf library. The complete C# code to get the HTML string rendered by the invoice page and to convert that string to PDF is:
// Execute the invoice page and get the HTML string rendered by this page
TextWriter outTextWriter = new StringWriter();
Server.Execute("Invoice.aspx", outTextWriter);
string htmlStringToConvert = outTextWriter.ToString();
// Create a HTML to PDF converter object with default settings
HtmlToPdfConverter htmlToPdfConverter = new HtmlToPdfConverter();
// Use the current page URL as base URL
string baseUrl = HttpContext.Current.Request.Url.AbsoluteUri;
// Convert the page HTML string to a PDF document in a memory buffer
byte[] outPdfBuffer = htmlToPdfConverter.ConvertHtml(htmlStringToConvert, baseUrl);
// Send the PDF as response to browser
// Set response content type
Response.AddHeader("Content-Type", "application/pdf");
// Instruct the browser to open the PDF file as an attachment or inline
Response.AddHeader("Content-Disposition", String.Format("attachment; filename=Convert_Page_in_Same_Session.pdf; size={0}", outPdfBuffer.Length.ToString()));
// Write the PDF document buffer to HTTP response
Response.BinaryWrite(outPdfBuffer);
// End the HTTP response and stop the current page processing
Response.End();
As long as you can make sure to use proper XHTML, you could also use a product like Alt-Soft's Xml2PDF to convert XML (XHTML) into PDF by means of XSLT/XSL-FO.
It takes a bit of a learning curve to master, but it works very well once you've "got" it!
Marc
Since you are producing the answer, you can use a tool like Report.NET:
http://sourceforge.net/projects/report/
I disagree with the answers that say you cannot convert directly from output to PDF, however, as you can "re-call" the page and get the HTML as a stream and convert it. I am not sure what tool you would want to use to do this, however. In other words, it is possible, but I am not sure it is worth it. The PDF creation libs, like Report.NET, even though they force reusing some logic and no automagic converrsion, it is easier.
I have not tried this component, but I have heard good things about it from those who have. The model is more like HTML, but I am not sure you can simply send a rendered ASPX to it to create PDF:
http://www.websupergoo.com/abcpdf-8.htm
If you try to find some html to pdf software via GOOGLE you'll get a pile of this stuff.
There are about 10 leaders but most of them use IE dlls in background mode.
Just couple of them use their own parsing engine.
Please try PDF Duo .NET component in your ASP.NET project if you wish to create a PDF programaticaly.
It is light component for a cool generating of PDF invoces, reports e.g.
I'd go a different route. Assuming you are using SQL Server, use SSRS and generate the PDF that way.
A possible minimal solution to use Server.Execute() to obtain the HTML of the invoice page and convert that code to a PDF using winnovative html to pdf api for .net is:
TextWriter outTextWriter = new StringWriter();
Server.Execute("Invoice.aspx", outTextWriter);
HtmlToPdfConverter htmlToPdfConverter = new HtmlToPdfConverter();
byte[] pdfBytes = htmlToPdfConverter.ConvertHtml(outTextWriter.ToString(),
httpContext.Current.Request.Url.AbsoluteUri);
You can use PDFSharp or iTextSharp to convert html to pdf. PDFSharp is not free.