Save PDF to variable
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.
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
Many thanks - switched over to PDFMake from JSPDF - this is a much easier product to code and style text.
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!
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.
Finally it's working! I had to exclude all jsPDF libraries from the project.
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?
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); });
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.
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);
});
});
}
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!
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.
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 ?
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 it is making it something like this
JVBERi0xLjMKJf////8KNiAwIG9iago8PAovVHlwZSAvRXh0R1N0YXRlCi9jYSAxCj4+CmVuZG9iago3IDAgb2JqCjw8Ci9UeXBlIC9FeHRHU3RhdGUKL0NBIDEKPj4KZW5kb2JqCjUgMCBvYmoKPDwKL1R5cGUgL1BhZ2UKL1BhcmVudCAxIDAgUgovTWVkaWFCb3ggWzAgMCA4NDEuODkgNTk1LjI4XQovQ29udGVudHMgMyAwIFIKL1Jlc291cmNlcyA0IDAgUgo+PgplbmRvYmoKNCAwIG9iago8PAovUHJvY1NldCBbL1BERiAvVGV4dCAvSW1hZ2VCIC9JbWFnZUMgL0ltYWdlSV0KL0V4dEdTdGF0ZSA8PAovR3MxIDYgMCBSCi9HczIgNyAwIFIKPj4KL0ZvbnQgPDwKL0YxIDggMCBSCi9GMiA5IDAgUgovRjMgMTAgMCBSCj4+Cj4+CmVuZG9iagozIDAgb2JqCjw8Ci9MZW5ndGggMjE4MDcKPj4Kc3RyZWFtCjEgMCAwIC0xIDAgNTk1LjI4IGNtCi9EZXZpY2VSR0IgY3MKMCAwIDAgc2NuCi9HczEgZ3MKZgpxCjEgMCAwIC0xIDAgNTk1LjI4IGNtCkJUCjQwIDU3MS4zNiBUZAovRjEgMTUgVGYKPDIxMjIyMzIyMjQyNTI0MjYyNzI4PiBUagpFVApRCi9EZXZpY2VSR0IgY3MKMCAwIDAgc2NuCi9HczEgZ3MKZgpxCjEgMCAwIC0xIDAgNTk1LjI4IGNtCkJUCjQwIDUyNC43MDggVGQKL0YyIDE0IFRmCjwyMTIyMjMyNDI1PiBUagpFVApRCi9EZXZpY2VSR0IgY3MKMCAwIDAgc2NuCi9HczEgZ3MKZgpxCjEgMCAwIC0xIDAgNTk1LjI4IGNtCkJUCjczLjExMzI4MTI1IDUyNC43MDggVGQKL0YyIDE0IFRmCjwyMjIzMjYyNzI4MjkyMjJhMjkyNzIzMjU+IFRqCkVUClEKL0RldmljZVJHQiBjcwowIDAgMCBzY24KL0dzMSBncwpmCnEKMSAwIDAgLTEgMCA1OTUuMjggY20KQlQKNDAgNDc4LjE1NTk5OTk5OTk5OTk1IFRkCi9GMSAxMiBUZgo8MjkyYTJiMmMyZD4gVGoKRVQKUQovRGV2aWNlUkdCIGNzCjAgMCAwIHNjbgovR3MxIGdzCmYKcQoxIDAgMCAtMSAwIDU5NS4yOCBjbQpCVAoyMDYgNDc4LjE1NT
but when i want to 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
`%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
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
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
@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)
});
How do you create pdfMake? I am getting error on getBase64 method.
@nfinfu, this way:
function sendPdf() {
pdfDocGenerator = pdfMake.createPdf(yourConfig);
return new Promise(function(resolve, reject) {
pdfDocGenerator.getBase64(function (pdfBase64) {
resolve(pdfBase64);
});
});
}