HummusJS
HummusJS copied to clipboard
Is it Possible to add jpg BASE64-strings to a PDF Base64-string?
Hello,
hopefully you can help me out on this. I have a certain service which gives me a base64 pdf string and 0-n base64 jpg strings.
Is it possible to append the base64 images to the base64 pdf using hummusJS?
Thanks for your answer!
#74 #77 #82
@chunyenHuang thanks you for your reply but I really mean: I got a pdf as a base64-string and several images as base64-string. In what way can I append the images to the pdf with (I am using node) using the base64-stream module for nodejs. I do see very little explanation on how to use the Streams with hummusJs. May be you have some small code examples? I am very new to this and would love to get some more help! Really appreciated. Thank you!
I forgot to mention: I donot want to write the file to the filesystem (= on the server) first and then create a stream. Kind regards, Pieter
For the pdf you would need to change your base64 PDF string to a buffer and then use:
const streams = require('memory-streams');
// pdf_buffer would be your base64 pdf string as a buffer
const inStream = new PDFRStreamForBuffer(pdf_buffer); //PDFRStreamForBuffer is here: https://github.com/galkahana/HummusJS/pull/202/files
const ws = new streams.WritableStream();
const outStream = new hummus.PDFStreamForResponse(ws);
const pdfWriter = hummus.createWriterToModify(inStream, outStream, );
// Now do stuff to the PDF as you normally would or as you see in all the examples
// once done:
pdfWriter.end();
return ws;
Once you've finished doing stuff to your PDF then ws
should contain your output buffer.
If you're in node then you should be able to do:
// const pdf = convertFromBase64ToBuffer(pdfBase64String);
const modifiedPDF = doPDFModification(pdf); //This function would have the code snippet above and do the pdf modification. modifiedPDF contains the returned ws from above.
res.attachment(result.filename);
res.send(modifiedPDF.toBuffer()).end();
Thank you @richard-kurtosys, I will give it a try!
@richard-kurtosys, all working until the pdfWriter. It is not created. Not knowing what is wrong there. Not logging: "after pdfWriter"
My code: ` var pdfBuffer = self.convertFromBase64ToBuffer(pdfBase64String); var inStream = new PDFRStreamForBuffer(pdfBuffer);
var ws = new memoryStreams.WritableStream();
console.log("ws: " + JSON.stringify(ws));
var outStream = new hummus.PDFStreamForResponse(ws);
// create the writer...not working.
var pdfWriter = hummus.createWriterToModify(inStream, outStream);
console.log("after pdfWriter");
`
Managed to get the errormessage: "Unable to modify PDF file, make sure that output file target is available and that it is not protected"
I'm not too sure what the issue could be there as it's definitely working for me.
You can try make your output go direct to a file, just to see that it does work correctly. If it does then you know the issue is definitely with the stream and not something else:
var pdfWriter = hummus.createWriterToModify(inStream, './output.pdf');
console.log("after pdfWriter");
You should now have a output.pdf
file somewhere with your output in it.
My package.json
entry for memory-streams
is: "memory-streams": "^0.1.3"
(maybe this helps?)
I've just had to convert a readStream to a buffer and then use that as an input, it looks like:
const pdfAsBuffer = await resolveReadStream(result.readStream);
const watermarked = watermarkDocument(pdfAsBuffer);
res.attachment("output.pdf");
res.send(watermarked.toBuffer()).end();
and then watermarkDocument has:
export function watermarkDocument(document) {
const inStream = new PDFRStreamForBuffer(document);
const readStream = new PDFRStreamForBuffer(document);
const ws = new streams.WritableStream();
const outStream = new hummus.PDFStreamForResponse(ws);
const pdfWriter = hummus.createWriterToModify(inStream, outStream );
....
....
pdfWriter.end();
return ws;
}
Thank you again @richard-kurtosys. I am using ^0.1.3 for the memory-streams. Will have a look in coming days. Now focussing on another client! Will come back on it.
Managed to get the errormessage: "Unable to modify PDF file, make sure that output file target is available and that it is not protected"
Hi, i have the same probleme, do you solve it?
This works well
const writeStream = new hummus.PDFStreamForResponse(res);
const pdfBuffer = new Buffer.from(base64string, 'base64');
const readStream = new hummus.PDFRStreamForBuffer(pdfBuffer);
const pdfWriter = hummus.createWriterToModify(readStream, writeStream, {
log: 'log.txt',
});
const pageModifier = new hummus.PDFPageModifier(pdfWriter, 0, true);
const doc = pageModifier.startContext().getContext();
doc.writeText('test'), 0, 200);
pageModifier.endContext().writePage();
pdfWriter.end();