【问题标题】:TCPDF table header apply a different margin on next page + closee table before page breakTCPDF 表格标题在分页符之前在下一页 + closee 表格上应用不同的边距
【发布时间】:2019-08-23 05:09:25
【问题描述】:

用于打印pdf的代码如下:

PdfController

/**
 * @Route("/{id}", name="api_article_pdf", methods={"GET"})
 */
public function gepdf2html($id)
{

    // create new PDF document
    $pdf = new \TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);

// set document information
    $pdf->SetCreator(PDF_CREATOR);
    $pdf->SetAuthor('OPAL TPE');
    $pdf->SetTitle('Articles');
    $pdf->SetSubject('TCPDF Tutorial');
    $pdf->SetKeywords('TCPDF, PDF, example, test, guide');

// set default header data
    $pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE . ' 001', PDF_HEADER_STRING, array(0, 64, 255), array(0, 64, 128));
    $pdf->setFooterData(array(0, 64, 0), array(0, 64, 128));

// set header and footer fonts
    $pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
    $pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));

 // set default monospaced font
    $pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);

// set margins
    $pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
    $pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
    $pdf->SetFooterMargin(PDF_MARGIN_FOOTER);

 // set auto page breaks
    $pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);

// set image scale factor
    $pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);


 // ---------------------------------------------------------

 // set default font subsetting mode
    $pdf->setFontSubsetting(true);

// Set font
// dejavusans is a UTF-8 Unicode font, if you only need to
// print standard ASCII chars, you can use core fonts like
// helvetica or times to reduce file size.
    $pdf->SetFont('dejavusans', '', 14, '', true);

// Add a page
// This method has several options, check the source code documentation for more information.
    $pdf->AddPage();

// set text shadow effect
    $pdf->setTextShadow(array('enabled' => true, 'depth_w' => 0.2, 'depth_h' => 0.2, 'color' => array(196, 196, 196), 'opacity' => 1, 'blend_mode' => 'Normal'));
// $id document
    $id_d= substr($id, 0,-7);
    $id=$id_d-5;
    $id=$id/7;
// Set some content to print

    // $em = $this->getDoctrine()->getManager();
    $query2= $this->em->createQuery('SELECT 
                                a.libelle as libelle,
                                a.description as desc,
                                a.prixVenteHt as prixHt,
                                a.prixVenteTtc as prixTtc
                                FROM
                                App\Entity\Dossier\Article as a
                                WHERE a.id = :id
                                ');
    $query2->setParameter("id", $id);
    $data = $query2->getResult();

    $html = $this->renderView('test.html.twig',
        ['data' => $data]);

// Print text using writeHTMLCell()
    $pdf->writeHTMLCell(0, 0, '', '', $html, 0, 1, 0, true, '', true);

// ---------------------------------------------------------

// Close and output PDF document
// This method has several options, check the source code documentation for more information.
    return $pdf->Output('example_001.pdf', 'I');

和我的 test.html.twig 代码:

<html>
<head>

</head>
<body>

<table nobr="true" cellspacing="0" cellpadding="1" border="1">
    <thead>
        <tr style="background-color:silver;"><th>Libelle</th>
            <td>Description</td>
            <td>Prix V HT</td>
            <td>Prix V TTC</td>
        </tr>
    </thead>

    <tbody>
    {% for d in data %}<br>
        <tr>
            <td>{{ d.libelle }}</td>
            <td>{{ d.desc }}</td>
            <td>{{ d.prixHt }}</td>
            <td>{{ d.prixTtc }}</td>
        </tr>
    {% endfor %}
    </tbody>
</table>


</body>

在第一页,一切正常,但在第一页中断后,表格标题浮动到左侧。

我尝试修复广告宽度以解决问题,但没有奏效。

我在堆栈或谷歌上尝试了非常解决方案,但没有更接近的结果

请教另一个问题:如何在 evreeey 分页符 (2) 之前关闭我的表格

PDF SCREENSHOT

非常感谢你:)

【问题讨论】:

    标签: symfony tcpdf margin-left


    【解决方案1】:

    我认为您也应该将表格的宽度固定为 % 和高度,所以我不会根据需要调整 ifself。 类似的东西:

    <table width="100%">
    <thead width ....
    <tr height="80">
    

    制作可以容纳数据的固定表和行,然后生成它,通过填充虚拟数据进行测试 编辑:对于 tr 中的 td 标签总和百分比 tso 它给你 100%

    【讨论】:

    • 谢谢你 Abderraxim,但它不起作用,通过检查 Chrome 中的源代码,解决方案只是删除 标签,所以我的树枝只包含 标签。
    • @zakymfony 是的,这行得通!只需删除除 之外的那些不必要的标签
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-06-29
    • 2022-07-22
    • 2012-12-25
    • 2012-03-14
    • 2023-03-17
    • 1970-01-01
    • 2017-10-05
    相关资源
    最近更新 更多