hummusRecipe icon indicating copy to clipboard operation
hummusRecipe copied to clipboard

Overlay PDF with multiple pages

Open danioso opened this issue 6 years ago • 2 comments

Is it possible to overlay more than one page from one pdf to another? I have a ten pages background pdf and a ten pages pdf for content each page is different.

Thanks

danioso avatar Jan 24 '18 06:01 danioso

It is possible to overlay multiple pdf in one page. The .overlay uses the drawImage from HummusJS reference. However, you may have to split your pdf and overlay each pdf to the related pages.

Or maybe I misunderstand your request, do you want to overlay multiple pages pdf into one page?

Or do you want to overlay specific page from the source pdf to the destination page?

chunyenHuang avatar Jan 24 '18 09:01 chunyenHuang

I needed something like this too, to overlay individual pages onto a master template, The only way I could solve this was to use hummus interfaces directly. See below. It would be a good addition to hummus-recipe

const PdfDocument = require('hummus-recipe');
const pdf         = require('hummus');

// -------------------------------------------------------------------------
// Generate PDF file from master PDF template and PDF data file.
// The master template is used as an underlay to the contents of
// the PDF data file. The master PDF template file may hold more than
// one page. The PDF data file may hold more pages than the master PDF
// file, but it is a requirement that the number of pages in the
// PDF data file be a multiple of the master PDF file's page count.
// For example, if the master holds 3 pages, the data file may hold 
// 3, 6, 9, etc.
//
// masterFile: path to PDF file holding document template (underlay)
// dataFile:   path to PDF file holding document data to be applied over template
// mergeFile:  path to newly generated file
// -------------------------------------------------------------------------
function merge(masterFile, dataFile, mergeFile){

    // Validate existence of expected parameters.
    if (! masterFile) {
        console.log("Missing PDF master template");
    } else if (! dataFile) {
        console.log("Missing PDF variable data file");
    } else if (! mergeFile) {
        console.log("Missing merge file name");
    } else {
        let masterSize = pageCount(masterFile);
        let dataSize   = pageCount(dataFile);

        if (dataSize % masterSize !== 0) {
            console.log("Variable data file page count is not multiple of master page count");
        } else {

            let merged   = pdf.createWriter(mergeFile);
            let pageInfo = getPageInfo(masterFile,1); // assuming all pages same size
            let width    = pageInfo.width;
            let height   = pageInfo.height;

            // ---------------------------------------------
            // Here is where pages are merged from the 
            // master PDf file and the given data PDF file.
            // ---------------------------------------------
            for(let d = 0; d < dataSize; d += masterSize) {
                for(let m = 0; m < masterSize; m++) {
                    let page = merged.createPage(0,0,width,height);
                    let p = d+m;
                    merged.mergePDFPagesToPage(page, masterFile, {type:pdf.eRangeTypeSpecific,specificRanges:[[m,m]]});
                    merged.mergePDFPagesToPage(page, dataFile,   {type:pdf.eRangeTypeSpecific,specificRanges:[[p,p]]});
                    merged.writePage(page).end;
                }
            }
            merged.end();
        }
    }
});

function getPageInfo(file, page) {
    let pdfDoc = new PdfDocument(file);
    let info = null;

    if (page === undefined) {
        page = 1;
    }
    try {
        info = pdfDoc.pageInfo(page);
    } catch (error) { }

    return info;
}

shaehn avatar Sep 26 '18 14:09 shaehn