Skip to main content
Version: Latest

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.

NameDescription
designThis is the JSON of the design
urlThis 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',
},
},
);

In the Document Builder, substituted values can change how tall each row is. When the values you pass differ from what the canvas displays (a merge tag's sample, or its name when it has no sample), or when a multi-language export uses a language other than the one shown on the canvas, the PDF is paginated server-side from the rendered content rather than the editor's currently-displayed pagination. Calls that pass no mergeTags keep the editor's pagination.


Page Format​

When using the Document Builder (displayMode: 'document'), you can override the page format that the PDF will be rendered at without modifying the saved design. This is useful for generating the same design at different paper sizes — for example, rendering a US customer's invoice as Letter and an EU customer's as A4 from a single template.

Availability

In the editor SDK and Export API v3, pageSize and documentOrientation overrides require a plan that includes PDF page-format overrides. Legacy plans use A4 portrait for document-mode exports. They can omit the fields or explicitly pass the A4 portrait no-op; a different format returns a 403 response.

The deprecated Export API v2 keeps its original pageSize contract for backward compatibility. Existing PDF customers, including legacy plans, can continue to use its full supported format list. The newer documentOrientation option still requires PDF page-format overrides.

All versions still require PDF export access.

OptionTypeDescription
pageSize'Letter' | 'Legal' | 'Tabloid' | 'Ledger' | 'A0' | 'A1' | 'A2' | 'A3' | 'A4' | 'A5' | 'A6'Paper size for the PDF. Requires PDF page-format overrides on v3; retained for existing PDF customers on v2.
documentOrientation'portrait' | 'landscape'Page orientation override. Requires a plan that includes PDF page-format overrides.

When pageSize is omitted, document-mode exports use the design's saved page size and orientation when the plan includes page-format controls. Projects without this feature use A4 portrait instead; non-default page values stored in their designs are ignored. The saved setting is named body.values.documentSize; it is not a separate export parameter. Saved formats use their standard physical paper dimensions consistently across Export API v2 and v3.

Page margin is derived from the selected format (e.g. A5 uses a tighter margin than A4) and isn't a configurable per-call option.

When pageSize is used without documentOrientation, it keeps the named PDF format's natural orientation instead of inheriting the orientation stored in the design. Ledger is naturally landscape; every other listed format is naturally portrait. Projects with PDF page-format overrides can pass documentOrientation: 'portrait' to rotate Ledger.

All plans with PDF export access can export without a per-call format override:

unlayer.exportPdf((data) => console.info(data));

Plans with PDF page-format overrides can select another format through the editor SDK or Export API v3:

unlayer.exportPdf((data) => console.info(data), { pageSize: 'Letter' });

Legacy integrations calling Export API v2 directly can also continue sending pageSize.

Unsupported pageSize values return a 400 response instead of falling back to another paper size.

unlayer.exportPdf(
function (data) {
var json = data.design;
var pdfUrl = data.url;
},
{
pageSize: 'Letter',
documentOrientation: 'landscape',
},
);

When an override changes the page geometry, the PDF is paginated server-side using the override's dimensions rather than the editor's currently-displayed pagination. The result will differ from what you see on the canvas if the override's geometry doesn't match the design's saved values — that's intentional.

Existing v2 integrations continue using pageSize for PDF export. New v3 integrations need PDF page-format override access to select a non-A4 document format.


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.info(json))
.catch((err) => console.error('error:' + err));

Learn more at Cloud API reference.

For document-mode designs, set displayMode: 'document'. This v2 endpoint preserves pageSize for existing PDF customers, including legacy plans. Passing documentOrientation still requires PDF page-format override access. On Export API v3, selecting a non-A4 document format requires that access as well. Page margin is derived from the selected format and isn't a configurable request field.

body: JSON.stringify({
displayMode: 'document',
design: {}, // json of your document design
pageSize: 'Letter',
documentOrientation: 'landscape',
});

Response​

The API response will return an uploaded pdf URL.

{
"success": true,
"data": {
"url": "https://.../1593082715347-8LCcNZuuBpU4D92W.pdf"
}
}