Pdfmake: Save locally

Created on 16 Jan 2015  路  11Comments  路  Source: bpampuch/pdfmake

Hello,

I'm trying to use pdfmake but I have one question about its utilisation. Can I just save my pdf file on my server without downloading it ?

I want my page to generate the pdf file and save it automatically in the folder "upload" of my server. Is it possible ?

Thank you

Most helpful comment

I found this works as a workaround:

    fs.writeFileSync('foo.pdf', new Buffer(new Uint8Array(buffer)));

All 11 comments

You can get an ArrayBuffer from pdfmake. This you can turn into a blob and post it to your server via HTTP.

pdfMake.createPdf(docDefinition).getBuffer(function(buffer) {
   // turn buffer into blob
   // send it to server via post
});

I can do it/save locally but I got broken in UTF-8 string. How to fix buffer data to UTF-8?

Thanks

I don't really understand your issue. Can you provide some example code and the exact error message?

I use it in node-webkit (NodeJS) :

pdfMake.createPdf(docDefinition).getBuffer(function(result) {
fs.writeFile('sample.pdf', result, function (err) {
if (err) throw err;
gui.Shell.openItem(afile);
});
}

The result is : all layout and fonts are OK, but string is broken.

error
Here is the screenshot (wrong and OK result)

Thanks

Hm, I don't see a problem in the picture ^^^

No seriously. I don't think using the browser-extension in node is the right way to do it. I have never used pdfmake on node, so I might have to look into it more closely. But my gut feeling would be to use something similar to https://github.com/bpampuch/pdfmake/blob/master/src/browser-extensions/pdfMake.js#L25 and follow the pdfkit api to actually store the file.

Like (untested ... so you might have to improve it):

var printer = new PdfPrinter(this.fonts);
var doc = printer.createPdfKitDocument(this.docDefinition, options);
doc.pipe(fs.createWriteStream('sample.pdf'));
doc.end();

BTW: the issue is probably not UTF-8. There is something wrong in binary conversion in node-webkit.

Thanks for your respond. But I got error in var printer = new PdfPrinter(this.fonts);

How to call PdfPrinter from pdfmake.js? I read in https://github.com/bpampuch/pdfmake/blob/master/examples/basics.js.

var PdfPrinter = require('../src/printer');

But it call from split source, how to call it from concatenated source?

thanks

You said : BTW: the issue is probably not UTF-8. There is something wrong in binary conversion in node-webkit.

But I get normal pdf in some doc using pdfMake.createPdf(docDefinition).download('currency.pdf',

I think it is not from node-webkit

I'm having the same problem when using Electron. pdfMake.createPdf(docDefinition).download('foo.pdf') works great but "download" interface is weird for a Electron app. When I try to write the stream to file instead though, the .pdf file's text is all wrong (looks very wrong-encoding to me).

// works!
pdfMake.createPdf(dd).download('foo.pdf');

// does not work :-(
pdfMake.createPdf(dd).getBuffer(function(buffer) {
fs.writeFileSync('foo.pdf', buffer);
});

I found this works as a workaround:

    fs.writeFileSync('foo.pdf', new Buffer(new Uint8Array(buffer)));

Thank you @seansbox. I was having this same issue and you saved me headaches with your writeFileSync workaround.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

kamilkp picture kamilkp  路  3Comments

einfallstoll picture einfallstoll  路  3Comments

michaelqiji picture michaelqiji  路  3Comments

m-brudi picture m-brudi  路  3Comments

svenyonson picture svenyonson  路  3Comments