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

Cannot add both text and image to existing PDF at the same time #210

Closed leewei05 closed 6 years ago

leewei05 commented 6 years ago

variable tmp is my input, Here is my code:

            var tmp=req.body.para;
            var pdfWriter = hummus.createWriterToModify('./contract.pdf', {
                modifiedFilePath: './pdf/' + 'modified_'+tmp.FileName+'.pdf'
            });       
            var pageModifier = new hummus.PDFPageModifier(pdfWriter,0);
            var textOptions = {font:pdfWriter.getFontForFile('./font/' + 'GenYoMinTW-
            Regular.ttf'),size:8,color:0x00};

            pageModifier.startContext().getContext().writeText(
                tmp.mileage_price.toString(),
                180, 405,
                textOptions
            );                
            pageModifier.endContext();   

            var cxt = pageModifier.startContext().getContext();
            cxt.drawImage(430,340,'./carImage'+tmp.signImage);
            pageModifier.endContext().writePage();                
            pdfWriter.end();

But it keeps console:

            TypeError: Unable to end PDF

I can work if I only modify text, but with image I get this error message. Please help!

galkahana commented 6 years ago

What if you just place the image?

leewei05 commented 6 years ago

@galkahana What do you mean by place the image?

galkahana commented 6 years ago

Do just the image adding part, without the text adding.

leewei05 commented 6 years ago

@galkahana I found the error, I misspelled my path to the image. Thank you though for the great function !