Jhtml2pdf Examples: Creating Stunning PDFs from HTML with Ease


What is Jhtml2pdf?

Jhtml2pdf is a Java library that leverages the power of Java’s Rich HTML and CSS support to create high-quality PDFs. It is based on the popular iText library and utilizes the htmlRenderer component from Flying Saucer to handle the rendering of HTML content. This makes it an ideal solution for developers looking to generate PDFs dynamically within their Java applications.

Key Features of Jhtml2pdf

  • HTML and CSS Support: It supports a wide range of HTML5 and CSS3 features.
  • Easy Installation: Quick to set up and integrate into existing Java projects.
  • Customizable: Allows for the customization of page sizes, orientations, and more.
  • Open Source: It is freely available, allowing for flexibility in usage and modification.

Installation

Step 1: Download Jhtml2pdf

You can download the Jhtml2pdf library from its official repository. Make sure to also include the iText library since it is a dependency.

Step 2: Add Dependencies

If you are using a build tool like Maven, add the following dependencies to your pom.xml:

<dependency>     <groupId>org.xhtmlrenderer</groupId>     <artifactId>flying-saucer-core</artifactId>     <version>1.1.22</version> </dependency> <dependency>     <groupId>com.itextpdf</groupId>     <artifactId>itextpdf</artifactId>     <version>5.5.13.2</version> </dependency> 
Step 3: Set Up Your Java Project

Make sure your IDE recognizes the library and its dependencies. Create a new Java class that will handle PDF generation.

Basic Usage

Now that you have installed Jhtml2pdf, let’s write some code to convert an HTML document to a PDF.

Example Code
import com.lowagie.text.Document; import com.lowagie.text.pdf.PdfWriter; import org.xhtmlrenderer.pdf.ITextRenderer; import java.io.FileOutputStream; public class PdfGenerator {     public static void main(String[] args) {         String htmlContent = "<html><body><h1>Hello, Jhtml2pdf!</h1></body></html>";         String pdfFilePath = "output.pdf";         try {             Document document = new Document();             PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(pdfFilePath));             document.open();             ITextRenderer renderer = new ITextRenderer();             renderer.setDocumentFromString(htmlContent);             renderer.layout();             renderer.createPDF(document);             document.close();             System.out.println("PDF successfully created at " + pdfFilePath);         } catch (Exception e) {             e.printStackTrace();         }     } } 

Adding Custom Styles

To customize the appearance of your PDF, you can include CSS styles within your HTML content. Here’s how you can do that:

String htmlContent = "<html><head><style>" +                      "h1 { color: blue; }" +                      "body { font-family: Arial, sans-serif; }" +                      "</style></head>" +                      "<body><h1>Hello, Jhtml2pdf!</h1></body></html>"; 

Advanced Features

Page Settings

You can adjust the page size and orientation by adding the following lines before creating the PDF:

renderer.getSharedContext().getUserAgentCallback()         .setPageSize(PageSize.A4); renderer.getSharedContext().setWidth(595); renderer.getSharedContext().setHeight(842); 
Handling Fonts

Jhtml2pdf also supports the embedding of custom fonts. To use custom fonts, ensure you have the font files accessible to your project. For example:

@font-face {     font-family: 'CustomFont';     src: url('path/to/CustomFont.ttf') format('truetype'); } 

Common Issues and Troubleshooting

  1. HTML Rendering Issues: Some complex CSS rules may not render as expected. Review the HTML and CSS for compatibility.
  2. Performance: For large documents, consider optimizing your HTML/CSS for better performance.
  3. Dependencies: Ensure that all necessary dependencies are included and that there are no version conflicts.

Conclusion

Jhtml2pdf is a powerful tool that can simplify the process of generating PDFs from HTML documents. Its ease of use, coupled

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *