0

用于打印pdf的代码如下:

Pdf控制器

/**
 * @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分页符之前关闭我的tablee(2)

PDF 截图

太感谢了 :)

4

1 回答 1

0

我认为您也应该将表格的宽度固定为 % 和高度,所以我不会将 ifself 调整为它想要的任何东西。就像是 :

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

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

于 2019-08-22T21:09:25.743 回答