使用Puppeteer生成PDF遇空白问题求助(CentOS 7环境)
Hey there, let's troubleshoot that blank PDF issue you're facing on CentOS 7! Since you already enabled --no-sandbox for kernel-related reasons, here are some common fixes to check:
1. Wait for the page to fully render before exporting
Most blank PDF cases happen because the export triggers before the page finishes loading/rendering. If you're using tools like Puppeteer (the most common choice for this task), make sure you wait for the page to be completely ready. Try using networkidle2 to wait for network activity to settle, or wait for a specific element that indicates the page is loaded:
const puppeteer = require('puppeteer'); (async () => { const browser = await puppeteer.launch({ args: ['--no-sandbox', '--disable-setuid-sandbox'] }); const page = await browser.newPage(); // Wait until network is nearly idle (only 2 requests in 500ms) await page.goto('http://example.com/', { waitUntil: 'networkidle2' }); await page.pdf({ path: 'example.pdf', format: 'A4' }); await browser.close(); })();
2. Fix missing system dependencies on CentOS 7
CentOS 7 comes with a minimal set of libraries, and headless Chromium (used by tools like Puppeteer) needs additional packages to render pages properly. Install these dependencies first:
yum install -y alsa-lib.x86_64 atk.x86_64 cups-libs.x86_64 gtk3.x86_64 ipa-gothic-fonts \ libXcomposite.x86_64 libXcursor.x86_64 libXdamage.x86_64 libXext.x86_64 libXi.x86_64 \ libXrandr.x86_64 libXScrnSaver.x86_64 libXtst.x86_64 pango.x86_64 xorg-x11-fonts-100dpi \ xorg-x11-fonts-75dpi xorg-x11-fonts-cyrillic xorg-x11-fonts-misc xorg-x11-fonts-Type1 xorg-x11-utils
If that doesn't work, add these GPU-related libraries (critical for headless rendering):
yum install -y libdrm mesa-libgbm
3. Use the system-installed Chromium instead of bundled versions
Sometimes the Chromium bundled with Puppeteer has compatibility issues on CentOS 7. Install the system's Chromium and point your tool to it:
First install Chromium:
yum install -y chromium
Then update your code to use the system executable:
const browser = await puppeteer.launch({ args: ['--no-sandbox', '--disable-setuid-sandbox'], executablePath: '/usr/bin/chromium-browser' // Path to system Chromium });
4. Debug JavaScript rendering issues
If the page relies heavily on JavaScript to render content, JS errors in headless mode could lead to blank PDFs. Try temporarily disabling headless mode to see the actual page (if your environment allows it) or check for console errors:
const browser = await puppeteer.launch({ args: ['--no-sandbox', '--disable-setuid-sandbox'], headless: false, // Show the browser window to check page load devtools: true // Open dev tools to inspect JS errors });
Also, add --disable-gpu to your args—CentOS 7 often lacks GPU support, which can break headless rendering:
const browser = await puppeteer.launch({ args: ['--no-sandbox', '--disable-setuid-sandbox', '--disable-gpu'] });
5. Rule out target website issues
Test PDF generation with a simple local HTML snippet first to confirm your tool works:
await page.setContent('<h1>Test PDF</h1><p>This is a working test document.</p>'); await page.pdf({ path: 'test.pdf', format: 'A4' });
If this works, the problem might be with example.com—try setting a desktop User-Agent to bypass any headless detection:
await page.setUserAgent('Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36');
内容的提问来源于stack exchange,提问作者M. Gara

