Home > Java > javaTutorial > How to Convert HTML to PDF While Respecting CSS Positioning Rules Using iText7?

How to Convert HTML to PDF While Respecting CSS Positioning Rules Using iText7?

DDD
Release: 2024-12-17 10:55:25
Original
113 people have browsed it

How to Convert HTML to PDF While Respecting CSS Positioning Rules Using iText7?

Converting HTML to PDF Using iText7

Question: How to Respect CSS Positioning Rules?

Previously, developers faced difficulties converting HTML to PDF using HTMLWorker due to deprecated methods and lack of CSS support. XMLWorker also had limitations, particularly with image positioning. To resolve these issues, iText 7 and the pdfHTML add-on were developed.

Answer:

The simplified approach to converting the provided HTML to PDF using iText 7.1.0 pdfHTML 2.0.0 is:

public static final String SRC = "src/main/resources/html/sample.html";
public static final String DEST = "target/results/sample.pdf";

public void createPdf(String src, String dest) throws IOException {
    HtmlConverter.convertToPdf(new File(src), new File(dest));
}
Copy after login

This results in a PDF that respects CSS positioning, including the image on the right.

Additional Thoughts:

  • Upgrade to iText 7 and pdfHTML 2 for enhanced features and support.
  • Respect the AGPL licensing if using iText for free in open source projects.
  • Commercial licenses are required for closed source/proprietary product use.

The above is the detailed content of How to Convert HTML to PDF While Respecting CSS Positioning Rules Using iText7?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template