pdfmake icon indicating copy to clipboard operation
pdfmake copied to clipboard

Save PDF to variable

Open bayhall opened this issue 11 years ago • 25 comments

Is it possible to save the created PDF to a local variable for use elsewhere, to send as an attachment for example, or encode as base64 - rather than print, download, or open.

bayhall avatar Feb 13 '15 12:02 bayhall

There are some possibilities: If you would like to have it as a dataURL do:

var data;
pdfMake.createPdf(docDefinition).getDataUrl(function(dataURL) {
    data = dataURL;
});

If you want to have only the base64 encoded string do:

var data;
pdfMake.createPdf(docDefinition).getBase64(function(encodedString) {
    data = encodedString;
});

If you want to have buffer (see #172): var data;

pdfMake.createPdf(docDefinition).getBuffer(function(buffer) {
    // turn buffer into blob
    data = buffer;
});

Hope it helps

Wabbala avatar Feb 13 '15 13:02 Wabbala

Many thanks - switched over to PDFMake from JSPDF - this is a much easier product to code and style text.

bayhall avatar Feb 13 '15 17:02 bayhall

Hi,

I struggle to get the base64 string out of my DocDefinition. This code works fine:

pdfMake.createPdf(doc).open();

But this code returns 'data is undefined':

var data;
pdfMake.createPdf(doc).getBase64(function(encodedString)
{data = encodedString;});

What am I doing wrong? Do I have to override the getBase64 function first or import anything? Thanks!

chr1sgar avatar Feb 16 '15 13:02 chr1sgar

Can't confirm - worked out the box for me - but I did also have this script https://github.com/dankogai/js-base64 left over from jspdf.

bayhall avatar Feb 16 '15 13:02 bayhall

Finally it's working! I had to exclude all jsPDF libraries from the project.

chr1sgar avatar Feb 16 '15 17:02 chr1sgar

with getDataUrl (but also with getBuffer and getBase64) i get only a "undefined".

var PDFdata;
pdfMake.createPdf(documentoPDF).getDataUrl(function(dataURL) {
PDFdata = dataURL;
});
console.log(PDFdata);

print in console: undefined

Any hel would be appreciate! :)

@chr1sgar how have you solved?

loverdrive avatar Jul 30 '15 14:07 loverdrive

I found you have to put the console.log inside the callback. Also, depending on the size of your pdf document it can take quite a long time for it to display on the console - my pdf was 2 pages of plaintext only and my console froze for a few minutes before displaying

var PDFdata; pdfMake.createPdf(documentoPDF).getDataUrl(function(dataURL) { PDFdata = dataURL; console.log(PDFdata); });

ttz21 avatar Aug 05 '15 15:08 ttz21

Hi all, I'm using angular js and pdfmake lib for pdf generation. I'm not using any other js. I'm getting following error: "Cannot read property 'createPdfKitDocument' of undefined" pls help on this.

umaganesan avatar Jun 24 '16 12:06 umaganesan

I found it easier in angular to wrap it in a promise using $q since getBase64 is an asynchronous function.

I called my report function like this:

createPDFReport().then(function(results) {
        vm.pdf = results;
     //Do stuff with your base64 string.  I needed to email 
        EmailService.sendEmail(vm.email,  vm.pdf);
      });

And structured the report function like this:

function createPDFReport() {
var docDefinition = {
...
...
...

return $q(function(resolve, reject) {
          pdfMake.createPdf(docDefinition).getBase64(function(encodedString) {
            resolve(encodedString);
          });
        });
}

Wombatpm avatar Jul 20 '16 20:07 Wombatpm

Thanks, for the explanation.

i have a question. After i saved the pdf in base64. how can i open it back to create a pdf?

Please help!

zirt0 avatar Oct 01 '16 11:10 zirt0

One I had the base64 string, I added it as an attachment to my email. The decoding was handled by the receiving email client. The base64 encoding is just a way to send a binary document over the internet via http and othe protocols. If you are doing everything local, you can save the file without encoding and open in a pdf reader.

Wombatpm avatar Oct 01 '16 16:10 Wombatpm

Can someone give me a sample code i have saved the pdf as base64 in my database(i used the code above). But when retrieve the string from the db endecode the base64 string it is giving me a weird code back. I can do nothing with that string. What am i doing wrong ?

zirt0 avatar Oct 01 '16 20:10 zirt0

Can you post some of your code? Otherwise you need to compare what you are putting into the database with what is coming out. It is possible that the round trip through the database is encoding a second time, decoding it, or something else.

Wombatpm avatar Oct 02 '16 15:10 Wombatpm

@Wombatpm it is making it something like this

JVBERi0xLjMKJf////8KNiAwIG9iago8PAovVHlwZSAvRXh0R1N0YXRlCi9jYSAxCj4+CmVuZG9iago3IDAgb2JqCjw8Ci9UeXBlIC9FeHRHU3RhdGUKL0NBIDEKPj4KZW5kb2JqCjUgMCBvYmoKPDwKL1R5cGUgL1BhZ2UKL1BhcmVudCAxIDAgUgovTWVkaWFCb3ggWzAgMCA4NDEuODkgNTk1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decode the base64, it is giving me this back

`%PDF-1.3 %ÿÿÿÿ 6 0 obj << /Type /ExtGState /ca 1

endobj 7 0 obj << /Type /ExtGState /CA 1

endobj 5 0 obj << /Type /Page /Parent 1 0 R /MediaBox [0 0 841.89 595.28] /Contents 3 0 R /Resources 4 0 R

endobj 4 0 obj << /ProcSet [/PDF /Text /ImageB /ImageC /ImageI] /ExtGState << /Gs1 6 0 R /Gs2 7 0 R

/Font << /F1 8 0 R /F2 9 0 R /F3 10 0 R

endobj 3 0 obj << /Length 21807

stream 1 0 0 -1 0 595.28 cm /DeviceRGB cs 0 0 0 scn /Gs1 gs f q 1 0 0 -1 0 595.28 cm BT 40 571.36 Td /F1 15 Tf <21222322242524262728> Tj ET Q /DeviceRGB cs 0 0 0 scn /Gs1 gs f q 1 0 0 -1 0 595.28 cm BT 40 524.708 Td /F2 14 Tf <2122232425> Tj ET Q /DeviceRGB cs 0 0 0 scn /Gs1 gs f q 1 0 0 -1 0 595.28 cm BT 73.11328125 524.708 Td /F2 14 Tf <222326272829222a29272325> Tj ET Q /DeviceRGB cs 0 0 0 scn /Gs1 gs f q 1 0 0 -1 0 595.28 cm BT 40 478.15599999999995 Td /F1 12 Tf <292a2b2c2d> Tj ET Q /DeviceRGB cs 0 0 0 scn /Gs1 gs f`

i am using angular base64 to decode

zirt0 avatar Oct 02 '16 16:10 zirt0

`%PDF-1.3 .... looks like a valid decode. So your issue has to be with the writing of the file.

So in my world I do everything in the client. Create the PDF convert to base64, do an http POST to my PHP server and then send the file as an attachment.

I'm assuming that you are doing everything in the client so you need to to use the Blob.js and a file system library of some sort. Check out https://www.npmjs.com/package/angular-file-saver.

Check out this fiddle as well http://jsfiddle.net/onury/5gWFL/ The only difference is that you'd set the type like

var file = new Blob([MyDecodedPDF], {type: 'application/pdf'});
 var fileURL = URL.createObjectURL(file);

If you are generating the PDF on a server and need to send it back the app then check out this stackoverflow question

Wombatpm avatar Oct 02 '16 20:10 Wombatpm

pdfMake.createPdf(dd).getDataUrl(function(dataURL) { data = dataURL; var blob = upload.dataUrltoBlob(data, 'testing'); uploadPdf(blob);

I'm currently facing a problem to get the pdf File properly, so i got on the server a file without extension and if i add a '.pdf' on my newFile it does not work better. It look like inside the file i didn't said that's a .pdf or something like that

ptitdje45 avatar Dec 23 '16 13:12 ptitdje45

Ok problem solved for me :

pdfMake.createPdf(dd).getDataUrl(function(dataURL) { data = dataURL; var blob = Upload.dataUrltoBlob(data, 'ficheSuivi.pdf'); uploadPdf(blob); });

This works like a charm <3

ptitdje45 avatar Dec 23 '16 13:12 ptitdje45

@zirt0, I had the same problem with bad decoding of base64 on server side.

Resolved problem this way (encode pdfBase64before sending to the server): return $.ajax({ url: 'send-email.php', type: 'POST', data: '[email protected]&pdfBase64=' + encodeURIComponent(pdfBase64) });

inovozenko avatar Jan 17 '17 14:01 inovozenko

How do you create pdfMake? I am getting error on getBase64 method.

nfinfu avatar Jun 20 '17 12:06 nfinfu

@nfinfu, this way:

function sendPdf() {
  pdfDocGenerator = pdfMake.createPdf(yourConfig);

  return new Promise(function(resolve, reject) {
    pdfDocGenerator.getBase64(function (pdfBase64) {
      resolve(pdfBase64);
    });
  });
}

inovozenko avatar Jun 20 '17 13:06 inovozenko