Skip to content
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

Open
stechio opened this issue Mar 1, 2021 · 5 comments
Open

Appending to a PDF document at arbitrary position on its last page #662

stechio opened this issue Mar 1, 2021 · 5 comments

Comments

@stechio
Copy link

stechio commented Mar 1, 2021

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):

lastPage

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?

@danfickle
Copy link
Owner

Hi @stechio,

You could try a margin-top for the first page. Something like:

@page:first {
  margin-top: 100mm; /* or 2in or 3cm or 100px or whatever */
}

@stechio
Copy link
Author

stechio commented Mar 1, 2021

Brilliant, thanks @danfickle!

@stechio
Copy link
Author

stechio commented Mar 1, 2021

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?

@danfickle
Copy link
Owner

You could use a dom mutator. DOM mutator example

@stechio
Copy link
Author

stechio commented Mar 1, 2021

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):

AppendLastPage_2

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
Generated PDF document: AppendLastPage_final.pdf

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants