PDFMerger(PHP)无法运行求助:执行代码后页面空白无输出
Hey there, let's work through why you're getting a blank page and no output—this is a common snag for new PHP developers working with PDF tools, so we'll take it step by step.
1. First, Turn on Error Reporting
Blank pages often mean PHP is throwing an error but not showing it. Add these lines at the very top of your script to reveal what's actually going wrong:
<?php error_reporting(E_ALL); ini_set('display_errors', 1); // rest of your code below
This will uncover hidden issues like missing files, path mistakes, or syntax errors that are currently invisible.
2. Fix the Critical Typo in Your Merge Call
I spotted a simple but fatal typo: you wrote 'brower' instead of 'browser' in the merge() method! That misspelled parameter is almost certainly causing the function to fail silently. Correct it to:
$pdf->merge('browser', 'samplepdfs/TEST2.pdf', 'P');
3. Verify PDFMerger's Dependencies
PDFMerger relies on FPDF and FPDI to handle PDF processing. If you only downloaded PDFMerger.php and not these two supporting libraries, the script will crash without warning:
- Check the top of
PDFMerger.phpfor lines likerequire_once('fpdf.php');orrequire_once('fpdi.php'); - Make sure those files exist in the same directory as
PDFMerger.php, or update the paths in therequire_oncestatements to point to where you’ve stored them.
4. Check File Permissions & Paths
- The
samplepdfsdirectory needs write permissions so PHP can save the mergedTEST2.pdffile. In MAMP, adjust permissions via your file manager (right-click the folder > Get Info > Permissions) or run this terminal command (replace the path with your actual project folder):chmod -R 755 /path/to/your/project/samplepdfs - For quick testing, skip saving to a file and send the PDF directly to your browser:
If this works, the issue was likely related to writing to the$pdf->merge('browser');samplepdfsdirectory.
5. Test with a Simplified Script
Strip down your code to the basics to rule out complex page range issues:
<?php error_reporting(E_ALL); ini_set('display_errors', 1); include 'PDFMerger.php'; $pdf = new PDFMerger; // Add just the first page of your PDF $pdf->addPDF('Starter.pdf', '1'); // Send directly to browser $pdf->merge('browser');
If this works, gradually add back your original addPDF() calls to narrow down if a specific page range is causing problems.
6. Confirm Your Source PDF is Valid
Double-check that Starter.pdf is a fully functional file—open it in a PDF reader to make sure it isn’t corrupted. Damaged PDFs will cause PDFMerger to fail without clear feedback.
内容的提问来源于stack exchange,提问作者Ben Derby

