galkahana / HummusJS

Node.js module for high performance creation, modification and parsing of PDF files and streams
http://www.pdfhummus.com
Other
1.14k stars 169 forks source link

Merge a page to an existing page. #146

Closed chunyenHuang closed 7 years ago

chunyenHuang commented 7 years ago

Hello Gal, Thanks for bringing us amazing HummusJS.

I just start to use this module, and need your help for merging existing pages.

I want to put overlay.pdf on top of background.pdf. Here is my current approach.

            const pdfReader = hummus.createReader(background);
            const pdfWriter = hummus.createWriter(output, {
                log: logFile
            });
            const bgContext = pdfWriter.createPDFCopyingContext(background);
            const olContext = pdfWriter.createPDFCopyingContext(overlay);

            for (var i = 0; i < pdfReader.getPagesCount(); i++) {
                const page = pdfWriter.createPage();
                page.rotate = pdfReader.parsePage(i).getRotate();
                page.mediaBox = pdfReader.parsePage(i).getMediaBox();
                bgContext.mergePDFPageToPage(page, i);
                olContext.mergePDFPageToPage(page, i);
                pdfWriter.writePage(page);
            }
            pdfWriter.end();

Apparently this is not a good idea, because copying context from both pdf takes more than 10 seconds for some large and complex pdf files.

I want to ask if there is any way that I can use

            const pdfWriter = hummus.createWriterToModify(
                background, {
                    modifiedFilePath: output,
                    log: logFile
                }
            );

and retrieve the inTargetPage for mergePDFPageToPage from this existing pdf file instead of getting from pdfWriter.createPage() ?? or this is also a wrong approach??

Thanks again for all your efforts.

galkahana commented 7 years ago

Merging pages is the most involved option of copying pages. Though i must say 10 secs is a lot in terms of anything that hummus does, so uts a bit suspicious.

One option is to use drawImage instead if merging. But yes, modifying is probably the best approach, yes. Use the background pdf as your modified source, and drawImage to place the overlay on top if it.

chunyenHuang commented 7 years ago

I thought I read all the documentation already, but I definitely missed drawImage .... Thanks for your advice and time, it runs perfectly right now.

eduardomourar commented 7 years ago

@galkahana, could I make a suggestion of renaming the drawImage() method to overlay() or something else? After reading the docs and going into C++ source code, I understand what the method is doing, but the original name is misleading. At first I thought it actually converted the overlaying PDF page to image, but they are kept as objects (+metadata) even if you run transformation (like rotating, scaling) on it.

galkahana commented 7 years ago

wouldn't go as far as changing the method name, but its a good idea to note this on the documentation. will do. thanks.