galkahana / HummusJS

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

Writing to Multiple Blank Pages with Hummus.js #306

Open SandMoshi opened 6 years ago

SandMoshi commented 6 years ago

I have long column like content that is being written to the left half of a page (page 1) but is overflowing so I need to create page 2 and finish writing the content to page 2 before going back to page 1 and writing to the right hand side.

I created code that will allow me to store the pages (page 1 and 2) in an array and also store the contexts in an array so that I can easily switch between the contexts I am writing to.

However, the new pages I create is empty even when I try to write to it's context

Here is the function that gets called when I'm ready to create page 2 and write a test statement on it.

  function createNewPage(){
        //Note all these variables are global, and hence I don't need to reinitialize them 

        if(lastPageNumber === 0 && currentPageNumber == 0){
            currentPageNumber++;
        }
        else{
            lastPageNumber++;
            currentPageNumber++;
        }
        //create a new page and store it in my array
        pagesArray[currentPageNumber] = pdfWriter.createPage(0, 0, eiReportPageWidth, eiReportPageHeight); 

        // create a new context and store it in my other array
        cxtArray[currentPageNumber] = pdfWriter.startPageContentContext(pagesArray[currentPageNumber]);

        //select the context I want to write to
        cxt = cxtArray[currentPageNumber];

        //I try writing to this new context but nothing happens and I get back a blank page

        cxt.writeText("Hello", 50, 400, fontOptions);
    }

//Now that all elements are in place, write to the page

pagesArray.forEach(page =>{
    pdfWriter.writePage(page);
})
galkahana commented 6 years ago

fairly sure this wont work. cant keep open contexts. you wanna pause them between switching. and while this technically should work it will create a bloated pdf. model you document separately, determining how each pages should look like, and only then write the pdf as a whole, page by page.

Gal.

SandMoshi commented 6 years ago

Thank You, that makes sense.