Electron PDF Printing and Report Export
🎯 Introduction
To export a report, prepare a print-friendly page and let Electron generate a PDF from it. Saving a screenshot of the current window serves a different purpose. This article implements a minimal page-to-PDF workflow.
🧱 Why use a dedicated report page?
An application page usually contains buttons, filters, and sidebars. Printing it directly includes those controls in the report. A dedicated report page can contain just headings, tables, and conclusions, making print styles easier to manage.
<!doctype html>
<html lang="en">
<body>
<main>
<h1>Document Analysis Report</h1>
<p>Generated on: 2026-09-17</p>
</main>
</body>
</html>
🛠 Use printToPDF
The workflow has five steps:
- Create a hidden window.
- Load the report page in that window.
- Convert the report page to PDF data.
- Let the user choose where to save it.
- Write the PDF data to the selected file, then close the hidden window.
The following function performs these five steps. It belongs in the main process because creating windows, opening save dialogs, and writing files are desktop capabilities:
const { BrowserWindow, dialog } = require('electron/main');
const path = require('node:path');
const { writeFile } = require('node:fs/promises');
async function exportReport() {
// 1. Create a window that remains hidden from the user
const reportWindow = new BrowserWindow({ show: false });
try {
// 2. Load the report page in the hidden window
await reportWindow.loadFile(path.join(__dirname, 'report.html'));
// 3. Generate PDF data from the report page
const pdf = await reportWindow.webContents.printToPDF({
printBackground: true,
pageSize: 'A4',
});
// 4. Open the system save dialog so the user can choose a location
const result = await dialog.showSaveDialog({
defaultPath: 'Document Analysis Report.pdf',
filters: [{ name: 'PDF files', extensions: ['pdf'] }],
});
// result.filePath is a valid save path only if the user did not cancel
if (!result.canceled) {
// 5. Write the binary PDF data to the selected file
await writeFile(result.filePath, pdf);
}
} finally {
// Close the hidden window whether the operation succeeds or fails
reportWindow.close();
}
}
The function does not run automatically. Call it when the user clicks “Export report”:
await exportReport();
printToPDF() returns binary PDF data. showSaveDialog() returns the path selected by the user, and writeFile() saves the PDF to disk.
If the report page uses web fonts or images, wait for them to finish loading before calling printToPDF(). Otherwise, the generated PDF may be missing fonts or images.
🧾 Summary
- Prepare a dedicated, print-friendly page for PDF export.
- Call
printToPDFin the main process. - Wait for fonts and images to load before exporting.
- Use the system save dialog to choose where to save the file.
- Close the export window promptly when finished.
❓ Questions and answers
Q1: Why not print the current window directly?
The current window usually contains buttons and filters. A report page gives you better control over content and styling.
Q2: How should PDF export failures be handled?
Catch the exception, return a clear error to the page, and ensure that the hidden window closes in finally.
🧪 Practice
Add a table to the report page and enable background printing in the export options:
const pdf = await reportWindow.webContents.printToPDF({
// Write your code here
});
🎉 You can now export a dedicated page as a PDF file.
Long-running Tasks and Performance Troubleshooting
Identify main process blocking, choose an appropriate approach to background tasks, and learn the basics of resource cleanup.
Application Packaging and Automatic Updates
Distinguish the responsibilities of package.json and forge.config.js, build an Electron app, and add basic automatic updates.
