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)?
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
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With