用于打印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>
在第一页上,一切正常,但是在第一页中断之后,表标题浮到了左侧。
我试图修复广告宽度以解决此问题,但这没用。
我在堆栈或Google上尝试了非常好的解决方案,没有更接近的结果
请问另一个问题:evreeey分页符(2)之前我怎么能关闭我的桌子
非常感谢您:)
答案 0 :(得分:0)
我认为您也应该将表格的宽度固定为%和高度,因此我不会根据自己的需要进行调整。 像
<table width="100%">
<thead width ....
<tr height="80">
使固定的表和行适合数据,然后生成它,并通过填充虚拟数据进行测试 编辑:对于tr标签中的td标签,百分比为tso,它为您提供100%