Live demos
Browse the sample list on the left and preview layouts plus sample code on the right. Run a demo to print silently or preview PDF via web-print-pdf and the local client. Install and launch the client first.
Client
If results look wrong, install the latest Web Print Expert client from our site first (older builds may cause layout drift).
Layout
Preview and print follow standard HTML/CSS only—no hidden tricks. What you style is what you get.
95 - Preview only
action: "preview" generates PDF only. Open printPreviewUrl in a new tab or iframe (call from a user click to avoid popup blockers).
Layout preview
Sample code
const res = await webPrintPdf.printHtml(html, { paperFormat: 'A4' }, {
// 实际打印时,须指定当前打印机上支持此尺寸的纸张名称(名称以 getPrinterPapers 返回为准),如果指定的纸张不符合此尺寸,效果是不对的。也可以不传递此参数,而是通过在当前软件打印机这个栏目的操作列,预先设置好 “打印首选纸张” 这个弹窗里的参数达到同样的效果
paperFormat: 'A4',
sharp: false /* 如果是热敏打印机,开启 sharp 抗锯齿,否则 false 即可 */
}, {
action: 'preview',
requestTimeout: 15
});
const previewUrl = res.data.printPreviewUrl;
if (!previewUrl) throw new Error('未返回预览地址');
// 展示方式由业务决定(请在「用户点击」回调里调用,避免 window.open 被拦截):
// 方式 A:新标签页打开 PDF(勿在 window.open 返回 null 后再 a.click,否则会开两个窗口)
window.open(previewUrl, '_blank', 'noopener,noreferrer');
// 方式 B:当前页 iframe 内嵌
// document.getElementById('pdf-preview').src = previewUrl;
Result
- If printing fails or the client cannot connect, see Local print works—production does not? (common on HTTP deployments).
- The client’s built-in Run examples page has more editable demos; this page complements it.
- Utility demos (connection, printer list) return JSON only—no print job.