Export PDF
Similar to the Export HTML function, you can also export a design in PDF format. This would upload the generated PDF to your connected File Storage and send the link to the callback function.
Note: Unlike HTML or plain text, PDF is not generated on the client. This function uses our cloud API to generate the PDF.
unlayer.exportPdf(function(data) {
var json = data.design; // design json
var pdfUrl = data.url; // pdf url
// Do something with the PDF url
})
Callback Parameters
The exportPdf callback returns the following parameters.
Name | Description |
---|---|
design | This is the JSON of the design |
url | This is the URL of the generated PDF |
Export Options
You can also pass certain options to this function as a second parameter.
Merge Tags
If you want the Merge Tags in your design to be replaced by different values, you can pass the mergeTags
object to export options.
unlayer.exportPdf(function(data) {
var json = data.design; // design json
var pdfUrl = data.url; // pdf url
}, {
mergeTags: {
first_name: 'John',
last_name: 'Doe'
}
})
Cloud API
If you want to generate the PDF on the server-side, you can use our Cloud API.
const fetch = require('node-fetch');
let url = 'https://api.unlayer.com/v2/export/pdf';
let options = {
method: 'POST',
headers: {'Content-Type': 'application/json', Authorization: 'Basic BASE64-ENCODED-API-KEY'},
body: JSON.stringify({
displayMode: 'email',
design: {}, // json of your design
mergeTags: {first_name: 'John', last_name: 'Doe'}
})
};
fetch(url, options)
.then(res => res.json())
.then(json => console.log(json))
.catch(err => console.error('error:' + err));
Learn more at Cloud API reference.
Response
The API response will return an uploaded pdf URL.
{
"success": true,
"data": {
"url": "https://.../1593082715347-8LCcNZuuBpU4D92W.pdf"
}
}
Updated about 2 months ago