Open sciurius opened 2 years ago
This is what I'm trying to achieve. I create the PDF document chunk-wise. Each chunk is one or more pages. Before each chunk there may be one blank pages for page alignment. Now if there is a blank page (I know that) and the chunk is two pages, I do not want the blank page. So I want to discard the last three pages, and re-process the chunk. Re-processing is necessary since the page headers and footers will be different (left/right, page numbers).
The hard way is to process each chunk to a temporary document, and then process it again to the main document, first inserting a blank page unless it is two pages.
Maybe there are other (better) ways to achieve this?
Some thoughts:
If I were writing this code (and without knowing the full context), I'd do two passes -- one to determine where blank pages are needed, and a second to do the actual generation. That may or may not be feasible in your case.
The PDF-preferred way to do this in one pass, based on my understanding of the spec, would be as you describe -- write the pages, then modify the page tree so that the unneeded pages are ignored (though not deleted). Unfortunately, PDF::API2 doesn't currently have handy tools for removing content or modifying the page stack other than adding/inserting pages.
Would you like to draft a $pdf->remove_page($page_number)
patch? The potential gotcha is that pages are stored in a tree rather than something simple like an array (see PDF 1.7 section 7.7.3 Page Tree). Other than that, it's probably not all that difficult to implement.
I did make some attempts some time ago but it turned out to be a bit more complicated than I anticipated. Given the alternatives I'll let this rest.
While creating a PDF document I create pages sequentially. At a certain point I decide that the last couple of pages that have been written are wrong, and I want to discard these and then add new pages. Since PDF::API2 does not have a remove_page method, what would be 'the right' way to obtain this? Do I need to manually manipulate the pagestack?