PDF Rendering
In the Document Builder (displayMode: 'document'), PDFs are rendered in the end-user's browser: exportPdf, the document preview, and the preview's Download PDF button. The PDF uses the same exported HTML, fonts, merge tags and language as a server export, and the same page format and margins.
The preview renders on every plan. exportPdf and Download PDF need a plan that includes PDF export. Without it, exportPdf receives an error instead of a url. On a free plan, Download PDF then saves the preview, which carries the Unlayer watermark; on other plans without PDF export, it isn't shown.
Rendering in the browser means:
- No export allowance. PDFs the editor renders don't count toward your workspace's export allowance. The Cloud API still renders PDFs on Unlayer's servers, and those count.
- Same result for
exportPdf. The PDF is stored in your File Storage like a server export, and the callback receives itsurl. In Unlayer's storage, a PDF rendered in the browser is deleted after 7 days, so save the file if you keep it longer (see Export PDF); in your own Amazon S3 bucket, it stays until you delete it, and so does every PDF Unlayer's servers render. PassdataUrl: trueto get the PDF itself instead, without uploading it. A PDF over 25 MB, one over the limit on uploads from one network, one the storage can't take at that moment (the upload takes over 2 minutes or fails on Unlayer's side), or a document too large for the browser renderer (hundreds of dense pages) is rendered and stored by Unlayer's servers instead, like a server export, and counts toward your export allowance. Download PDF of a document too large for the browser renderer also comes from Unlayer's servers. In offline mode (theoffline: trueinit option, used with an offline license and locally bundled assets), nothing is uploaded:urlis adata:URL of the PDF and the result includesoffline: true. - Fewer round trips. The preview and Download PDF don't wait on the export service.
- Files loaded ahead. Once the design hasn't changed for a couple of seconds, the editor reads its fonts and the images it can read directly, so the first PDF waits less for them. The browser keeps them for as long as their servers allow, as it does any file. Your
fetchImagecallback and the image proxy are never called ahead: what only they can read loads when a PDF is built. Download PDF saves the PDF the preview shows when nothing changed since the preview built it and all its fonts, images and effects loaded. A document whose custom tools or HTML blocks run scripts, link or import a stylesheet of their own, frame another page, or use an SVG sprite sheet from another file is built again, since those can give something else each time.
What the browser draws
A background image repeated in more than a million tiles, such as a dot smaller than a pixel across a page, is drawn once, and may look slightly different from the editor. The browser console says so.
CSS effects that PDF boxes can't hold, such as blur, drop-shadow and SVG filters on a box, masks, clip-paths, 3D transforms and vertical text, are drawn as images the browser paints, as Chrome prints filters, and so are boxes inside a line of text that hold blocks or are transformed. Their text is written over the image, word by word where the image shows it, so it can still be found, selected and copied. Color filters such as grayscale and sepia, flat transforms, rounded clips and blend modes are drawn as they are.
A block taller than a page breaks across pages as when a browser prints the document: boxes side by side, such as a row's columns or a table's cells, break each on its own lines, CSS columns fill each page's columns in turn, and a table repeats its header and footer on each page, when each is at most a quarter of the page's height for content.
The browser renderer's own failures don't go to Unlayer's servers: when its code can't be downloaded, it fails on a document, or it takes over 2 minutes, exportPdf receives an error instead of a url, and the preview and Download PDF show it.
A browser can't measure a page it isn't laying out, which is what Chrome does for an editor hidden with display: none, such as one in a closed tab or panel, including one hidden while a PDF is being built. Unlayer's servers then render the PDF, and it counts toward your export allowance. An editor hidden any other way, for example with visibility: hidden or moved off-screen, still renders in the browser. Offline or with dataUrl, where nothing may be uploaded, exportPdf receives an error instead: show the editor and try again.
In offline mode, an image the browser can't load stops the PDF: the exportPdf callback receives an error instead of a url, and the preview and Download PDF show the reason; see Images.
On a free plan, every page carries the Unlayer watermark. The browser draws it on the preview, Download PDF and dataUrl PDFs, as Unlayer's servers stamp it. When a free plan includes PDF export, exportPdf without dataUrl is rendered on Unlayer's servers, which stamp the watermark, and counts toward your export allowance. On plans that include Document Watermarks, a custom watermark, passed to the call or set as the features.watermark default, is drawn in the browser, exactly where and how the servers would stamp it.
The plan decides the watermark, so the editor creates document PDFs only once it has loaded your plan. A PDF asked for while the project loads waits for it, up to 15 seconds. If the plan can't be loaded, for example because the editor couldn't start its session or runs offline without a license, exportPdf receives an error instead of a url, and the preview and Download PDF show it. If unlayer.loadProject fails afterwards, PDFs keep using the plan of the project the editor started with.
Fonts
The PDF uses the document's web fonts, including your custom fonts. Serve a custom font's files with CORS headers, as browsers require for web fonts. Its stylesheet doesn't need them: when the editor can't read it directly, it reads it through Unlayer's image proxy (not in offline mode). A font the editor can't read is replaced by the next font in its list. Every list ends with an open fallback font; if it can't be loaded either, the PDF's text can't be drawn, so Unlayer's servers render the PDF instead, and it counts toward your export allowance. Offline or with dataUrl, exportPdf receives an error instead.
System fonts can't be downloaded by the browser, so the PDF draws them with openly licensed fonts:
- Arial, Helvetica, Times New Roman, Courier New, Andale Mono, Georgia, Arial Black and Comic Sans MS are drawn with fonts whose letters have the same widths, so text wraps as in the editor. A few characters of the last three differ in width, such as fractions, some accented capitals in Georgia and the non-breaking space in Comic Sans MS, and text holding one can wrap differently.
- Impact is drawn with a close equivalent, and Verdana, Tahoma, Trebuchet MS, Book Antiqua, Symbol and Terminal with a similar sans-serif, serif or monospace font. Their text can wrap differently than in the editor.
To print one of those exactly, add your licensed copy of the font as a custom font.
Text in scripts whose letters join, stack or reorder, such as Arabic, Urdu, Hebrew, the Indic scripts, Thai, Lao, Khmer, Myanmar and Tibetan, is shaped by HarfBuzz, the engine browsers use, so it looks and wraps as in the editor. The editor loads it (about 170 KB) only for a document with such text. If it can't, for example because the download fails, that text is still drawn and the browser console says so, but some letter clusters can differ from the editor.
The renderer runs WebAssembly, which it also loads fonts with. If a Content Security Policy applies to the page that runs the editor, its script-src must allow 'wasm-unsafe-eval'. Without it, the fonts can't be loaded, so Unlayer's servers render every PDF, and they count toward your export allowance; offline or with dataUrl, exportPdf receives an error instead of a url.
When a font has no bold or italic file, the PDF fakes the bold or slants the text, as browsers do.
Characters none of the document's fonts has, such as emoji and some symbols, are drawn as small images, in the fonts the browser shows them in on screen. They can still be selected, searched and copied as text.
Custom tools
Custom tools render in the browser from the HTML their exporter returns, like the built-in tools:
- Shadows. Box shadows and text shadows are drawn. PDF text can't be blurred, so a blurred text shadow is drawn sharp and lighter.
- Form controls and embedded content. Fields, buttons, drop-down lists, checkboxes, radio buttons, sliders, progress bars and meters are drawn as Chrome prints them. A video shows its
posterimage. A frame shows the page written into it, withsrcdocor by a script. A frame of another site, which browsers don't let a page read, an<object>or<embed>that isn't an image, and an empty<canvas>keep an empty box their size. - Scripts. Scripts in the exported HTML run before the PDF is drawn, as they do on Unlayer's servers. They run in an isolated frame, with no access to the editor, its storage or the end-user's session, until the page stops changing and has no request, timer, interval or animation frame left to wait for, for up to 5 seconds. They get storage and cookies of their own, kept only while they run. The PDF shows the page they leave, with what they typed, ticked or chose in form fields, even if a script fails or is still working after 5 seconds, and anything they drew on a
<canvas>becomes an image. If the editor's page doesn't allow inline scripts in frames, for example because of a Content Security Policy, the PDF shows the page without running them. The browser console and thewarningsof theexportPdfresult say what went wrong. A script that never returns, such as an endless loop, freezes the page it runs in, as it does on the canvas: the browser can't stop it.
Images
The browser loads each image directly when its host sends CORS headers. When it can't, it asks your fetchImage callback, if you registered one, and then Unlayer's image proxy (not in offline mode). An image none of them can load is left empty, as on the canvas when its own server refuses it (for example with a 404), and the browser console and the warnings of the exportPdf result say so. An image whose host doesn't answer has no size the PDF could keep for it: it's read the same ways, and when none of them loads it, it's left out, and the warnings say so.
Register fetchImage to load images from hosts without CORS headers through your own backend. The callback receives the image's URL and calls done with the image as a Blob, or with an error, including when your backend answers with an error status, so the editor goes on to the image proxy:
unlayer.registerCallback('fetchImage', function (data, done) {
fetch('/my-image-proxy?url=' + encodeURIComponent(data.url))
.then((response) => {
if (!response.ok)
throw new Error('Image request failed: ' + response.status);
return response.blob();
})
.then((blob) => done({ blob }))
.catch((error) => done({ error }));
});
Each step has a time limit: 20 seconds to load directly, 20 seconds for fetchImage to call done, and 25 seconds through the image proxy. Images over 25 MB are redrawn smaller to fit, and images over 100 MB are left empty.
In offline mode, there's no image proxy and no server to fall back to: an image the browser can't read, directly or through fetchImage, stops the PDF, and exportPdf returns an error that says so. An image its own server refuses is still left empty. Register fetchImage, or serve your images with CORS headers.