-
Notifications
You must be signed in to change notification settings - Fork 170
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to create buffer #125
Comments
Hi @dottodot. The principal is pretty much the same - instead of writing to a file, you want to write to a custom write stream implemented for writing to buffers. buffers are constant size, and you don't know the PDF length in advance. so what you want to use is some writable stream that can in the end be turned into a buffer. For instance, consider using memory-stream. Here is the response writing example written with var hummus = require('hummus'),
stream = require('memory-streams');
var writer = new streams.WritableStream();
var pdfWriter = hummus.createWriter(new hummus.PDFStreamForResponse(writer));
var page = pdfWriter.createPage(0,0,595,842);
pdfWriter.startPageContentContext(page).writeText('Hello World'),
0,400,
{
font:pdfWriter.getFontForFile('../tests/TestMaterials/fonts/arial.ttf'),
size:50,
colorspace:'gray',
color:0x00
});
pdfWriter.writePage(page);
pdfWriter.end();
// at this point you can use the stream methods to create buffer or string:
// Output the content as a string
console.log(writer.toString());
// Output the content as a Buffer
console.log(writer.toBuffer()); Hope this helps, |
@galkahana, I have implemented this approach in a project and it is working quite well. The only issue is that I've noticed that there's a memory leak somewhere. I'll follow up with a code example soon but I'm curious if you have any ideas where that might be coming from? Thanks. |
Okay, I actually have 2 separate functions creating pdfs and I'm not sure which one the memory leak is coming from. This creates a single pdf var writer = new streams.WritableStream();
var pdfWriter = hummus.createWriter(new hummus.PDFStreamForResponse(writer));
var page = pdfWriter.createPage(0, 0, 612, 792);
var cxt = pdfWriter.startPageContentContext(page);
var regularFont = pdfWriter.getFontForFile(__dirname + '/../public/fonts/lato/Lato-Regular.ttf');
cxt.writeText('Text', 0, 0, { font: regularFont, size: 12, colorspace: 'rgb', color: 0x000000 });
pdfWriter.writePage(page);
pdfWriter.end();
var buffer = writer.toBuffer(); This combines multiple pdfs var pdfBuffers = [array of buffers loaded using Parse Server httpRequest];
var writer = new streams.WritableStream();
var pdfWriter = hummus.createWriter(new hummus.PDFStreamForResponse(writer));
_.each(pdfBuffers, function(pdfBuffer) {
var pdfReadStream = new PDFRStreamForBuffer(pdfBuffer);
pdfWriter.appendPDFPagesFromPDF(pdfReadStream);
});
pdfWriter.end();
var buffer = writer.toBuffer(); Do I need to clear anything out of memory? |
@galkahana my string buffer is empty, did i do something wrong ?
|
@nooballday Did you ever figure this out? I have the same issue. |
@capndave i ended up saving the file first then delete it after i am done with the file. |
Sorry finding it a bit hard to work out from the documentation but I'm trying to modify a pdf, but rather than saving it to disk I need to be able to create a Buffer which can then be used as an email attachment.
This is what I have so far which works but write to disk
The text was updated successfully, but these errors were encountered: