使用TCPDF的writeHTML生成PDF时出现文本重叠问题求助
I’ve run into this exact bold text overlapping issue with TCPDF before, and it almost always comes down to font width calculation mismatches when switching between regular and bold styles in inline HTML. Here’s how to fix it without modifying your existing HTML content:
Step 1: Explicitly Load the Bold Font Variant
TCPDF sometimes doesn’t automatically recognize the bold version of the default Helvetica font, which leads to incorrect character width calculations when rendering font-weight: bold inline styles. Add this right after initializing your TCPDF object to ensure the bold font is registered:
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false); // Load Helvetica Bold explicitly first $pdf->SetFont('helvetica', 'B', 12); // Switch back to regular Helvetica for your base text $pdf->SetFont('helvetica', '', 12, '', true);
By explicitly setting the bold font first, TCPDF will properly map the font-weight: bold style to the correct font file, ensuring accurate width calculations for both regular and bold text.
Step 2: Disable Font Subsetting (If Needed)
Font subsetting (the final true in your SetFont call) can sometimes corrupt font metrics and cause layout issues. Try disabling it temporarily to see if that resolves the overlap:
$pdf->SetFont('helvetica', '', 12, '', false);
If this fixes the problem, you can re-enable subsetting later, but you may need to verify your font files are fully compatible with TCPDF’s subsetting logic.
Step 3: Adjust Character Spacing (Quick Workaround)
If the above steps don’t work, a small adjustment to character spacing can compensate for remaining width calculation errors. Add this line right before your writeHTML call:
$pdf->setFontSpacing(0.2); // Tweak the value (0.1-0.5 usually works)
This adds a tiny gap between characters, preventing bold text from overlapping with adjacent regular text.
Bonus: Update TCPDF to the Latest Version
Older TCPDF releases had known bugs with inline CSS font weight handling. If you’re using an outdated version, upgrading to the latest release may resolve the issue immediately.
Modified Full Code with Fixes
Here’s your code with the recommended fixes applied:
<?php include('PDF/tcpdf.php'); $pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false); $pdf->SetCreator(PDF_CREATOR); $pdf->SetAuthor('test'); $pdf->SetTitle("test"); $pdf->setPrintHeader(false); $pdf->setPrintFooter(false); $pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED); $pdf->SetMargins(20, PDF_MARGIN_TOP, 20); $pdf->SetHeaderMargin(PDF_MARGIN_HEADER); $pdf->SetFooterMargin(PDF_MARGIN_FOOTER); $pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM); $pdf->setImageScale(PDF_IMAGE_SCALE_RATIO); // Explicitly load Helvetica Bold first $pdf->SetFont('helvetica', 'B', 12); // Revert to regular Helvetica for base text $pdf->SetFont('helvetica', '', 12, '', true); $pdf->AddPage(); $html = '<div>Die Wohnung des Hauptmieters befindet sich in der <span style="font-weight: bold;">This text is supposed to space properly but is overlapping.</span> in <span style="font-weight: bold;">I don\'t know why, can anyone help me here?</span> in der <span style="font-weight: bold;">It would be greatly appreciated.</span> Etage. Es wird ein Raum zu Wohnzwecken und zur ausschließlichen Nutzung an den Untermieter vermietet. Der Vermieter hat der Untervermietung schriftlich zugestimmt. </div>'; // Optional: Adjust character spacing to prevent overlap $pdf->setFontSpacing(0.1); $pdf->writeHTML($html, true, false, true, false, 'L'); $pdf->Output(__DIR__ . '/test.pdf', 'I'); ?>
The explicit bold font loading should resolve the overlapping issue for most cases. Give it a shot!
内容的提问来源于stack exchange,提问作者Anurag Srivastava

