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

Refactor PDF exports for headless printing #891

Draft
wants to merge 9 commits into
base: master
Choose a base branch
from

Conversation

sixlettervariables
Copy link
Contributor

@sixlettervariables sixlettervariables commented Mar 12, 2021

This PR refactors PDF exports to more easily allow headless printing.

Here is an example of it in use from a web service:

    private byte[] createRecordSheet(LambdaLogger logger, Entity entity) {
        try {
            return new RecordSheetBuilder().setEntity(entity).buildPdf();
        } catch (Exception e) {
            // ...
        }
    }

    @Override
    protected void handleRequest(...) {
        // ...
        String category = getParameter(event, "category");
        String unitPath = getParameter(event, "unit");
        String path = String.format("%s/%s", category, unitPath);

        Entity entity;
        try {
            entity = s3.readEntity(importBucket, path);
        } catch (EntityLoadingException | SdkException e) {
            // ...
        }

        // ...

        Util.ensureHeadless();
        Util.ensureLocalFonts(logger);

        byte[] pdfBytes = createRecordSheet(logger, entity);
        if (pdfBytes != null) {
            builder.setContentType(ContentTypes.PDF);
            builder.setStatusCode(200).setBinaryResponse(pdfBytes);
        } else {
            ErrorResponse response = new ErrorResponse("Could not create record sheet for " + path);
            builder.setStatusCode(500).setJsonResponse(response);
        }
    }

Copy link
Member

@neoancient neoancient left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When I try to print a test 200-page document I get to about 14% (very slowly) and it runs out of memory and barfs.

src/megameklab/com/printing/PdfRecordSheetExporter.java Outdated Show resolved Hide resolved
for (int i = 0; i < rs.getPageCount(); i++) {
merger.addSource(rs.exportPDF(i, pageFormat));
}
iter.remove();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The sheet instances take up a lot more memory when processed, and large jobs can easily run out of memory. I tried clearing generated data from the PrintRecordSheet instance without success. The only way I found to handle the memory problems is to remove the sheets once they're added to the document. I don't see any reason the book needs to be reusable.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll test this with a large document tonight, good to know that was the issue.

I found some other items that kept large amounts of memory rooted and will put up a separate PR.

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

Successfully merging this pull request may close these issues.

2 participants