Methods
Base usage
This call will generate a PDF document, methods to get document are described below.
pdfMake.createPdf(docDefinition);
pdfMake.createPdf(docDefinition, options);
Parameters:
docDefinition- object with document definition, see chapteroptions(optional) - advanced options see options chapter
Download the PDF
pdfMake.createPdf(docDefinition).download();
pdfMake.createPdf(docDefinition).download('file.pdf');
Parameters:
filename(optional) - PDF document file name (default ‘file.pdf’)
Open the PDF in a new window
pdfMake.createPdf(docDefinition).open();
pdfMake.createPdf(docDefinition).open(win);
Parameters:
win(optional) - window (when an asynchronous operation)
Name can be defined only by using metadata title property (see Document metadata).
Asynchronous example:
$scope.generatePdf = function() {
// create the window before the callback
var win = window.open('', '_blank');
$http.post('/someUrl', data).then(function(response) {
// pass the "win" argument
pdfMake.createPdf(docDefinition).open(win);
});
};
Open in same window:
pdfMake.createPdf(docDefinition).open(window);
Print the PDF
pdfMake.createPdf(docDefinition).print();
pdfMake.createPdf(docDefinition).print(win);
Parameters:
win(optional) - window (when an asynchronous operation)
Asynchronous example:
$scope.generatePdf = function() {
// create the window before the callback
var win = window.open('', '_blank');
$http.post('/someUrl', data).then(function(response) {
// pass the "win" argument
pdfMake.createPdf(docDefinition).print(win);
});
};
Print in same window:
pdfMake.createPdf(docDefinition).print(window);
Get the PDF document as URL data
Example of put the PDF document into your own page as URL data:
pdfMake.createPdf(docDefinition).getDataUrl().then((dataUrl) => {
const targetElement = document.querySelector('#iframeContainer');
const iframe = document.createElement('iframe');
iframe.src = dataUrl;
targetElement.appendChild(iframe);
}, err => {
console.error(err);
});
Get the PDF document as base64 data
pdfMake.createPdf(docDefinition).getBase64().then((data) => {
alert(data);
}, err => {
console.error(err);
});
Get the PDF document as Blob
pdfMake.createPdf(docDefinition).getBlob().then((blob) => {
// ...
}, err => {
console.error(err);
});
Get the PDF as buffer
pdfMake.createPdf(docDefinition).getBuffer().then((buffer) => {
// ...
}, err => {
console.error(err);
});
Get the PDF document as stream
pdfMake.createPdf(docDefinition).getStream().then((stream) => {
// ...
}, err => {
console.error(err);
});
URL Access Policy
Minimal version: 0.3.6
The setUrlAccessPolicy() method allows you to define a custom security policy for external URLs before they are downloaded.
This can be used to restrict allowed domains, enforce HTTPS, or prevent SSRF attacks by blocking private or internal network addresses.
For details see documentation.