How to convert an HTML page to a PDF file using Java

I searched the web how to convert an HTML page to a PDF using Java. I found a lot of pointers and, in short, they do not work or they are too difficult to implement. I also downloaded a commercial product, pdf4ml; The API is something that I would be happy to work with, except that when I look at a simple page in wikipedia, I get an error from memory (setting Xmx to 1024 M). in some approaches, they suggest converting HTML β†’ XHTML β†’ FO β†’ PDF. however, I get many exceptions for the XHTML-to-FO XLS file; and reading documentation, it’s not that I have enough time to understand right now.

here are my questions / concerns. 1. Is there another cohesive API that easily converts HTML to PDF (commercial or not)? 2. There is a way that I can just grab the HTML page and save it as a single file. Will this approach be similar to using Internet Explorer to save a web page as a web archive (single file, MHT format)?

any help is appreciated. (By the way, I know that this question has been asked repeatedly, but in addition to the original spirit of the question, I am open to other ways). thanks.

+4
source share
5 answers

Try wkhtmltopdf , which uses WebKit. Another option (I'm currently using this) uses OpenOffice (remote control via macros).

+4
source

you can use open source java lib lib for this and read this

or use the open source Java lib YaHPConverter .

or do it with icepdf a popular open source library

or use pd4ml , but it's not free, just a trial version.

or use this , and this is the person for him.

+1
source

My 2 cents using open source tools:

You can use Capture Screenshots with Selenium or WebDriver to save a html screenshot in an image file from your Java code. And once you have the image file, you can convert it to PDF again from Java code .

EDIT: It seems you can do it all in one step using itext Html to Pdf

0
source

I'm not sure, but you could try

1) cobra html rendering engine http://lobobrowser.org/cobra.jsp

2) htmleditorkit - part of jdk

3) JWebPane

Use the rendering set to parse and render html. The highlighted pin is a swing component. Swing component can be used by itext to generate a PDF file placed

0
source

You can try Pdfcrowd . It is an easy-to-use commercial online API with many options and Java support.

It can create PDF either from web pages or from raw HTML code.

0
source

Source: https://habr.com/ru/post/1344724/


All Articles