Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
435 views
in Technique[技术] by (71.8m points)

php - Why does TCPDF add weird top space in my PDF?

I want to use TCPDF to export html to PDF but I'm getting some weird space at the top of the document. I think I've set all Margin and Padding options to zero but I cannot get rid of it. Can anyone help me? I've added a red border top to the main container and h1 to see where it is aligned. As you can see in the screenshot there is space on top of them.

For the record:

  • Laravel 8
  • Php 7.4
  • TCPDF 6.3.2

My PDF settings:

$pdf = new TCPDF($config['orientation'], 'pt', $pageLayout, true, 'UTF-8', false);
$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
$pdf->SetMargins(0, 0, 0,true);
$pdf->setCellPaddings(0,0,0,0);
$pdf->setCellMargins(0,0,0,0);
$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
$pdf->SetPrintHeader(false);
$pdf->SetPrintFooter(false);
$pdf->AddPage();
$pdf->writeHTML($html, false, false, false, false, 'top');
$fileName = date('YmdHis').'-document.pdf';
return $pdf->Output($fileName, 'I');

Here is my CSS:

*{
    margin:0;
    padding:0;
}
@page{
    size: 21cm 24cm;
    margin:0;
    padding:0;
    clear: both;
}
html, body {
    width:617px;
    height:617px;
    margin:0;
    padding:0;
    clear: both;
}
.print-page-container {
    clear: both;
    width:617px;
    margin:0;
    padding:0;
    font-family: 'D-DIN', sans-serif;
    font-size: 12px;
    background:#fff;
    color:#000;

    border-top:1px solid #ff0000;
}
.h1 {
    clear: both;
    font-family: 'D-DIN', sans-serif;
    font-size: 40px;

    border-top:1px solid #ff0000;
}

Here is my Html

<body>
<div class="print-page-container">
    <div class="h1">Colli ID: #06021</div>
</div>
</body>

And a screenshot: Strange margin top

question from:https://stackoverflow.com/questions/65679907/why-does-tcpdf-add-weird-top-space-in-my-pdf

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Jeej fixed it by adding:

$tagvs = array(
    'div' => array(0 => array('h' => 0, 'n' => 0), 1 => array('h' => 0, 'n' => 0))
);
$pdf->setHtmlVSpace($tagvs);

TCPDF adds spacing to div's.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...