Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert an HTML content to PDF without losing the formatting using Java?

I have some HTML content (including formatting tags such as strong, images etc).In my Java code, I want to convert this HTML content into a PDF document without losing the HTML formatting.

Is there anyway to do it in Java (using iText or any other library)?

like image 589
Veera Avatar asked Jan 17 '11 11:01

Veera


1 Answers

I used ITextRenderer from the Flying Saucer project.

Here is a short, self-contained, working example. In my case I wanted to later stream the bytes into an email attachment.

So, in the example I write it to a file purely for the sake of demonstration for this question. This is Java 8.

import com.lowagie.text.DocumentException;
import org.apache.commons.io.FileUtils;
import org.xhtmlrenderer.pdf.ITextRenderer;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;

public class So4712641 {

  public static void main(String... args) throws DocumentException, IOException {
    FileUtils.writeByteArrayToFile(new File("So4712641.pdf"), toPdf("<b>You gotta walk and don't look back</b>"));
  }

  /**
   * Generate a PDF document
   * @param html HTML as a string
   * @return bytes of PDF document
   */
  private static byte[] toPdf(String html) throws DocumentException, IOException {
    final ITextRenderer renderer = new ITextRenderer();
    renderer.setDocumentFromString(html);
    renderer.layout();
    try (ByteArrayOutputStream fos = new ByteArrayOutputStream(html.length())) {
      renderer.createPDF(fos);
      return fos.toByteArray();
    }
  }
}

This gives me

enter image description here

For completeness, here are relevant pieces for my Maven pom.xml

<dependencies>
    <dependency>
        <groupId>org.xhtmlrenderer</groupId>
        <artifactId>flying-saucer-pdf</artifactId>
        <version>9.0.8</version>
    </dependency>
    <dependency>
        <groupId>commons-io</groupId>
        <artifactId>commons-io</artifactId>
        <version>2.4</version>
    </dependency>
</dependencies>
like image 102
Kirby Avatar answered Sep 20 '22 12:09

Kirby