Skip to content
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

Open
dottodot opened this issue Dec 13, 2016 · 6 comments
Open

How to create buffer #125

dottodot opened this issue Dec 13, 2016 · 6 comments

Comments

@dottodot
Copy link

dottodot commented Dec 13, 2016

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();
@galkahana
Copy link
Owner

galkahana commented Dec 13, 2016

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:

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.

@enjoyjeremy-bc
Copy link

enjoyjeremy-bc commented Mar 24, 2017

@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.

@enjoyjeremy-bc
Copy link

enjoyjeremy-bc commented Mar 25, 2017

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?

@nooballday
Copy link

nooballday commented Jul 2, 2018

@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

@capndave
Copy link

capndave commented Oct 9, 2018

@nooballday Did you ever figure this out? I have the same issue.

@nooballday
Copy link

@capndave i ended up saving the file first then delete it after i am done with the file.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants