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

Cant modify WS PDF response in base64 string in Node #296

Open JCarlosGarza opened 6 years ago

JCarlosGarza commented 6 years ago

OK, first sorry for my bad english, I'll try to explain myself. I have a PDF response (in base64 literal string), generated by a webservice. I want to add some text or background image or something that prints on top of the original PDF so it says "NO OFICIAL". I already tried overlaying another PDF, appending a pdf with the text and creating the original with stream. I don't want to store the files generated so I tried the solution at issue #241 with the code:

// b64Final is the string substring value of the pdf webservice response
bufferPdf = Buffer.from(b64Final, "base64");
const inStream: any = new hummus.PDFRStreamForBuffer(bufferPdf);
const ws: any = new streams.WritableStream();
const outStream: any = new hummus.PDFStreamForResponse(ws);
const pdfWriter: any = hummus.createWriterToModify(inStream, outStream, );
var pageModifier = new hummus.PDFPageModifier(pdfWriter, 0);
pageModifier.startContext().getContext().writeText("QwErT", 270, 420);
// can't get font to work FATAL ERROR: v8::ToLocalChecked Empty MaybeLocal
pageModifier.endContext().writePage();
pdfWriter.end();
res.send(ws.toBuffer());

Its size is different than the original. I already tried issue #160 and #242 too. I even tried Hummus-Recipe npm package and other alternatives. I'm worried that the pdf is damaged. I hope you can help me.

The next pdf is what I got after the modification process. Boleta de Calificaciones (1).pdf

And this one is the ws original response. ReporteBimestral-3546011-2018.pdf

Thanks a lot.