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 resource caching
The setUrlResourceCaching(enabled) method controls whether new URL downloads are stored in a shared in-memory cache for reuse by subsequent documents. The enabled parameter is required and must be a boolean. URL resource caching is disabled by default, so downloads are stored per document by default.
pdfMake.setUrlResourceCaching(true);
pdfMake.createPdf(docDefinition).download();
Set cache: true or cache: false on an individual URL resource object to override the global setting. Existing shared entries can still be read with cache: false; it does not force a new download. This setting does not control the browser’s HTTP cache.
For resource examples, cache behavior and the change from earlier versions, see URL resource caching.
Clear URL resource cache
The clearUrlResourceCache() method synchronously removes shared HTTP/HTTPS resources without changing the caching setting or removing other VFS files. It takes no parameters. Call it between document generations, after active generations have finished:
pdfMake.clearUrlResourceCache();
pdfMake.createPdf(docDefinition).download();
This method does not clear the browser’s HTTP cache. For details see Clear URL resource cache.
URL Access Policy
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.