Closed. This question is seeking recommendations for software libraries, tutorials, tools, books, or other off-site resources. It does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for software libraries, tutorials, tools, books, or other off-site resources. You can edit the question so it can be answered with facts and citations.
Closed 2 years ago .I am looking for a way to convert an HTML file to PDF using a Java library that is preferably free. I have done some searching online to look for tools to use, but haven't found a solution that sticks out (I have seen some mention of iText, but it looked like that would have a charge to use it). Is there an existing library that I can utilize to accomplish the conversion of HTML to PDF?
asked Aug 12, 2016 at 18:06 Developer Guy Developer Guy 2,424 6 6 gold badges 21 21 silver badges 38 38 bronze badgesYou have a few options:
It was very simple to get this to work for me, here is a method I created to use this:
public static void generatePDF(String inputHtmlPath, String outputPdfPath) < try < String url = new File(inputHtmlPath).toURI().toURL().toString(); System.out.println("URL: " + url); OutputStream out = new FileOutputStream(outputPdfPath); //Flying Saucer part ITextRenderer renderer = new ITextRenderer(); renderer.setDocument(url); renderer.layout(); renderer.createPDF(out); out.close(); >catch (DocumentException | IOException e) < // TODO Auto-generated catch block e.printStackTrace(); >>
And here is the usage:
public static void main(String[] args) < String inputFile = "C:/Users/jrothst/Desktop/TestHtml.htm"; String outputFile = "C:/Users/jrothst/Desktop/TestPdf.pdf"; generatePDF(inputFile, outputFile); System.out.println("Done!"); >
It worked very well to output the PDF and was very simple to use. It also handled the CSS in the html pretty well. Didn't use it for external CSS, but I believe that is possible too.
answered Aug 23, 2016 at 15:21 Developer Guy Developer Guy 2,424 6 6 gold badges 21 21 silver badges 38 38 bronze badgesI like Flying Saucer, too. Just be aware that while FS has a friendly license, it uses iText 5 (e.g. that ITextRenderer in your code sample), which has an AGPL license. Because of that, I ended up going with openhtmltopdf, because it has NO iText dependencies (it uses Apache PDFBox). It played quite nicely with CSS, and did everything that I needed it to do, so far.
Commented Aug 1, 2018 at 23:03 Please provide code of your implementation @yngwietiger Commented Aug 20, 2018 at 7:42 Unfortunately Flying Saucer does not support CSS3 Commented Sep 26, 2019 at 14:06How about "org.xhtmlrenderer:flying-saucer-pdf-openpdf" github.com/flyingsaucerproject/flyingsaucer. Does this also use itext?
Commented Mar 8, 2021 at 15:16Here is the complete conversion of html file to pdf file working example.
import com.itextpdf.text.Document; import com.itextpdf.text.html.simpleparser.HTMLWorker; import com.itextpdf.text.pdf.PdfWriter; import java.io.IOException; import java.io.FileReader; import java.io.Reader; import java.io.BufferedReader; import java.io.File; import java.io.FileOutputStream; import java.io.OutputStream; import java.io.StringReader; import org.jsoup.Jsoup; public class Html2pdf2 < private Html2pdf2() <>public static String extractText(Reader reader) throws IOException < StringBuilder sb = new StringBuilder(); BufferedReader br = new BufferedReader(reader); String line; while ( (line=br.readLine()) != null) < sb.append(line); >String textOnly = Jsoup.parse(sb.toString()).text(); return textOnly; > public final static void main(String[] args) throws Exception < FileReader reader = new FileReader ("example.html"); try < OutputStream file = new FileOutputStream(new File("D:\\Test.pdf")); Document document = new Document(); PdfWriter.getInstance(document, file); document.open(); HTMLWorker htmlWorker = new HTMLWorker(document); htmlWorker.parse(new StringReader(ht)); document.close(); file.close(); >catch (Exception e) < e.printStackTrace(); >System.out.println("finished converting"); > >
answered Dec 14, 2017 at 7:45
yatheendra k v yatheendra k v
99 1 1 silver badge 7 7 bronze badges
Can you explain your code with some comments to understand the process plz?
Commented Jul 6, 2020 at 14:07
@IgniteCoders The above code makes use of the iText library using which you can have the conversion done. Currently am using it to succeed. HTMLWorker class does the Job.