I'm trying to upgrade to the latest version of iText and I'm running into some issues. Currently we use Apache Velocity templates, and then convert the resulting HTML into a pdf using iText.
We have a large image(2220 x 3248) that is included on the PDF as a background. I've cut down the template to a bare minimum test.
<html>
<head>
<style type="text/css">
@page {
margin-top:57px;
margin-bottom:50px;
margin-left: 110px;
margin-right: 40px;
padding-top:160px;
padding-bottom:5px;
background-image:url('templates/background.png');
background-size:100% 100%;
background-position:top left;
}
</style>
</head>
<body>
<h1>Background Image Test</h1>
</body>
</html>
I then use the following code to render out the PDF.
public void render(PdfRendererContext ctx) throws Exception {
ConverterProperties properties = new ConverterProperties();
PdfWriter writer = new PdfWriter((OutputStream) ctx.getOutput().getOutput());
PdfDocument pdf = new PdfDocument(writer);
properties.setBaseUri("path/to/files");
HtmlConverter.convertToPdf((String) ctx.getInput().getInput(), pdf, properties);
}
This converts the PDF and I do mostly get what I want, but the background image is no longer scaled. The image is supposed to be a border around the page with a logo in the middle, but as you can see the image is much larger than the total page.
I've tried changing Background-size a few different ways but with no success. Is there any way to scale a background image similar to what I need?
Unfortunately, pdfHTML does not support background-size
CSS property just yet. There is a solution with post-processing that you can use to add background images to your PDF document with iText Core. So you won't have any background images defined in HTML and instead you will just add them on the post-processing stage. Here is how the code looks for the described approach:
ByteArrayOutputStream baos = new ByteArrayOutputStream();
// Converting to a memory stream to process the document in memory afterwards before flushing to disk
HtmlConverter.convertToPdf(htmlStream, baos);
PdfDocument pdfDocument = new PdfDocument(new PdfReader(new ByteArrayInputStream(baos.toByteArray())),
new PdfWriter(new File("C:/path/to/out.pdf")));
ImageData imageData = ImageDataFactory.create("C:/path/to/background.png");
// Page size of the output HTML document
Rectangle pageSize = PageSize.A4;
Image image = new Image(imageData).scaleAbsolute(pageSize.getWidth(), pageSize.getHeight());
for (int i = 1; i <= pdfDocument.getNumberOfPages(); i++) {
PdfPage page = pdfDocument.getPage(i);
// newContentStreamBefore just adds some content on the back layer, to be shown behind other content
PdfCanvas pdfCanvas = new PdfCanvas(page.newContentStreamBefore(), page.getResources(), pdfDocument);
new Canvas(pdfCanvas, pdfDocument, pageSize).add(image);
}
pdfDocument.close();