-
Notifications
You must be signed in to change notification settings - Fork 365
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Appending to a PDF document at arbitrary position on its last page #662
Comments
Hi @stechio, You could try a @page:first {
margin-top: 100mm; /* or 2in or 3cm or 100px or whatever */
} |
Brilliant, thanks @danfickle! |
Considering that the HTML I feed to the layout engine comes from a static file, what's the most convenient way to inject the CSS snippet you suggested here above? Do I have to load and alter the HTML before feeding it to the engine or can I apply the additional CSS directly to the engine? |
You could use a dom mutator. DOM mutator example |
Thanks to your hints, I managed to generate an almost-correct result: the new contents are perfectly appended to the original last page, but the exceeding contents are laid out on the next page without honoring the page margin (despite having explicitly set it via DOM mutator -- see code below): Here it is the generation code: File outputFile = new java.io.File("AppendLastPage_final.pdf");
try (OutputStream os = new FileOutputStream(outputFile)) {
new PdfRendererBuilder()
.usePDDocument(PDDocument.load(new java.io.File("AppendLastPage_original.pdf")))
.usePageSupplier((doc, pageWidth, pageHeight, pageNumber, shadowPageNumber) -> {
PDPage page;
if (pageNumber == 0) {
// First appending page is last original page.
page = doc.getPage(doc.getNumberOfPages() - 1);
} else {
doc.addPage(page = new PDPage());
}
return page;
})
.withFile(new java.io.File("append.html"))
.addDOMMutator(doc -> {
Element style = doc.createElement("style");
Node node = doc.createTextNode(
"@page:first { margin-top: 200mm; } "
+ "@page { margin-top: 15mm; }");
style.appendChild(node);
doc.getElementsByTagName("head").item(0).appendChild(style);
}).toStream(os).run();
} Appended HTML source (append.html): <html> <head> </head> <body> <p>** BEGIN APPEND **</p> <h1>New contents appended to the original document</h1> <p>Some text to fill the space Some text to fill the space Some text to fill the space Some text to fill the space Some text to [cut] space Some Some text to fill the space Some Some text to fill the space Some Some text to fill the space Some</p> <p>** END APPEND **</p> </body> </html> Original PDF document: AppendLastPage_original.pdf |
My use case is about appending the rendering to an existing PDF document, with the special requirement of starting such addition from the blank area left on its last page, as shown in the following example (gray area stands for the trailing contents of the existing PDF document, green area for the blank space where the layout engine should start to build the rendering):
PageSupplier
seems the way to go, but it doesn't allow to constrain the layout to an arbitrary area (expressed as a rectangular shape or, at least, a vertical coordinate where to start inserting contents) within the supplied page.Is it possible to achieve my goal with the current implementation?
The text was updated successfully, but these errors were encountered: