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
var pdfWriter = hummus.createWriterToModify(__dirname + '/voucher.pdf', {
modifiedFilePath: __dirname + '/TestPageModified.pdf'
});
var pageModifier = new hummus.PDFPageModifier(pdfWriter, 0);
var fontOswald = pdfWriter.getFontForFile('.fonts/Oswald-Bold.ttf');
pageModifier.startContext()
.getContext()
.writeText(
'拢100',
296, 105, {
font: fontOswald,
size: 70,
colorspace: 'gray',
color: 0x00
}
).writeText(
'CODE: GHGJHJHH',
296, 80, {
font: fontOswald,
size: 18,
colorspace: 'gray',
color: 0x00
}
);
pageModifier.endContext().writePage();
pdfWriter.end();
Hi @dottodot.
What you need is something simmilar to what happens in this example which writes a pdf to an http response stream. instead of writing to a response stream you want to write to a buffer.
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.
memory-stream.WritableStream is a writable stream class that you can create and object from.
you don't have to build hummus custom stream to use memory-stream.WritableStream, because actually hummus.PDFStreamForResponse can write to any node Stream.Writable.
Here is the response writing example written with memory-streams:
````javascript
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,
Gal.
@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 ?
```
let hummus = require('hummus')
let stream = require('memory-streams')
, fs = require('fs')
let ws = new stream.WritableStream();
let writer = hummus.createWriterToModify('./contoh.pdf', new hummus.PDFStreamForResponse(ws))
let pageModifier = new hummus.PDFPageModifier(writer, 0);
let arialFont = writer.getFontForFile('./arial.ttf');
pageModifier.startContext().getContext()
.writeText(
'拢100',
296, 105, {
font: arialFont,
size: 70,
colorspace: 'gray',
color: 0x00
}
).writeText(
'CODE: GHGJHJHH',
296, 80, {
font: arialFont,
size: 18,
colorspace: 'gray',
color: 0x00
}
);
pageModifier.endContext().writePage();
writer.end();
console.log(ws.toString());
console.log(ws.toBuffer());
```javascript
@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.
Most helpful comment
Hi @dottodot.
What you need is something simmilar to what happens in this example which writes a pdf to an http response stream. instead of writing to a response stream you want to write to a buffer.
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.
memory-stream.WritableStreamis a writable stream class that you can create and object from.you don't have to build hummus custom stream to use
memory-stream.WritableStream, because actuallyhummus.PDFStreamForResponsecan write to any nodeStream.Writable.Here is the response writing example written with
memory-streams:````javascript
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,
Gal.