I want to create pdf by passing web URL with out open browser. IS it possible? any example.
-
2It's not entirely clear what you are trying to achieve. Can you edit your question to give an example of what you want to achieve, what you have tried, and where exactly you are having trouble? – Lars Kristensen Aug 14 '17 at 06:46
-
I want to create pdf by passing url in background and save it in local folder. i dont want to open browser and to create pdf . – Srinivas Ch Aug 14 '17 at 06:52
-
As an alternate you can check [Rotativa](https://github.com/webgio/Rotativa/blob/master/README) where there is a possibility to generate PDF from Route/Url as well. Also check [this SO](https://stackoverflow.com/questions/4014149/how-to-convert-url-of-html-page-to-pdf-in-java-using-itext-flying-saucer) if it help you. – Siva Gopal Aug 14 '17 at 07:29
-
Yes, that's possible with iText 7 and the pdfHTML add-on. – Bruno Lowagie Aug 14 '17 at 15:03
1 Answers
Let's take this page as an example, and let's write a method named createPdf()
that takes a URL
pointing at the web address and a String
representing the path of the resulting PDF. That method could be called like this:
app.createPdf(
new URL("https://stackoverflow.com/questions/45668769"),
"stackoverflow_question45668769.pdf");
The simplest way to implement this method looks like this:
public void createPdf(URL url, String dest) throws IOException {
HtmlConverter.convertToPdf(url.openStream(), new FileOutputStream(dest));
}
We don't need a browser, the URL
object opens an InputStream
that reads the bytes straight from the web server. Those bytes are parsed by the pdfHTML add-on. No browser or WebKit-like software is needed.
The result looks like this:
That's great, but there's a slight problem: the default page size used by iText 7 is A4, and there's not enough space to accommodate all the content.
Let's rotate the page, and let's define a media query that tells iText more about the "screen size" (which is, in our case, actually a page size):
public void createPdf(URL url, String dest) throws IOException {
PdfWriter writer = new PdfWriter(dest);
PdfDocument pdf = new PdfDocument(writer);
PageSize pageSize = PageSize.A4.rotate();
pdf.setDefaultPageSize(pageSize);
ConverterProperties properties = new ConverterProperties();
MediaDeviceDescription mediaDeviceDescription = new MediaDeviceDescription(MediaType.SCREEN);
mediaDeviceDescription.setWidth(CssUtils.parseAbsoluteLength(String.valueOf(pageSize.getWidth())));
properties.setMediaDeviceDescription(mediaDeviceDescription);
HtmlConverter.convertToPdf(url.openStream(), pdf, properties);
}
Now the result looks like this:
Pretty neat, isn't it?
Stack Overflow also has a CSS that is used when you print a page. So let's create one more variation of the createPdf()
method:
public void createPdf(URL url, String dest) throws IOException {
ConverterProperties properties = new ConverterProperties();
MediaDeviceDescription mediaDeviceDescription = new MediaDeviceDescription(MediaType.PRINT);
properties.setMediaDeviceDescription(mediaDeviceDescription);
HtmlConverter.convertToPdf(url.openStream(), new FileOutputStream(dest), properties);
}
Now we tell iText that we are creating the PDF for print instead of for the screen. The result looks like this:
Do you see the difference? The previous PDFs all consisted of several pages. This PDF is only one page long, because the print.css
used by Stack Overflow is defined in such a way that all unnecessary information for a printed version of the question is removed.
Note:
I used Java code because I am a Java developer, not a C# developer. However: iText 7 and pdfHTML for Java are (almost) identical to iText 7 and pdfHTML for C#. All iText development is originally done in Java, and then autoported to C#. The method names you'll need are identical, except for the lowercase that is changed into an uppercase for the first character of the method names. You'll also have to adapt the Stream
objects. This shouldn't be a problem for a C# developer.
Important:
If you are using HTMLWorker
to convert HTML to PDF, abandon all hope. HTMLWorker
was written to convert small snippets of HTML code to PDF. The code of HTMLWorker
was written in a quick and dirty way, and it couldn't be extended without throwing away large part of the code. That's why HTMLWorker
was abandoned. It is no longer supported and it should no longer be used.
It you are using XML Worker, please understand that XML Worker is based on iText 5. The design of iText 5 predates the idea of building an HTML to PDF converter. The architecture of iText 5 wasn't well suited for the purpose of converting web pages to PDF. Please don't expect results similar to the ones shown in the screen shots added to this answer with iText 5 and XML Worker.
We rewrote iText from scratch with HTML to PDF conversion in mind. The result was iText 7. If you want to execute the code shown in this answer, you will also need the pdfHTML add-on to iText 7. You will need a (trial) license key to write this code.
The license can be loaded like this:
LicenseKey.loadLicenseFile(pathToLicenseKey);
where pathToLicenseKey
is the path to the XML file you obtained when you registered for a trial license or when you purchased a commercial license for your use of iText.

- 75,994
- 9
- 109
- 165
-
PdfWriter writer = new PdfWriter(dest); code says "'PdfWriter' does not contain a constructor that takes 1 argument" – Golda Jun 15 '18 at 11:45
-
1Which version of iText are you using? See the API docs of iText 7.1.2: http://itextsupport.com/apidocs/iText7/latest/com/itextpdf/kernel/pdf/PdfWriter.html#PdfWriter-java.io.File- There are three constructors that take 1 argument, hence your comment is incorrect. – Bruno Lowagie Jun 15 '18 at 12:58
-
Thanks for your kind replay, I have installed it from NuGet. Version 5.5.13. I am using the package to create PDF using URL in console app. – Golda Jun 15 '18 at 13:08
-
Version 5.5.13 is a maintenance version of the old iText. It's created for paying customers who can't upgrade to the most recent version. iText 7 is also on NuGet. You need `Install-Package itext7`. See https://developers.itextpdf.com/itext7/download-and-install-information/NET – Bruno Lowagie Jun 15 '18 at 13:25