php 导出pdf

  1、接口生成查询参数

$exportCS = urlSafeB64Encode(msgpack_pack(['key' => 'value']))

2、解析查询参数,获取查询数据结果
$condition = msgpack_unpack(urlSafeB64Decode($exportCS))
3、安装依赖
composer require tecnickcom/tcpdf
4、生成pdf并下载
$pdf       = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
// set document information
$pdf->SetCreator(PDF_CREATOR);
$pdf->SetTitle('标题');
//不打印头和页脚
$pdf->setPrintHeader(false);
$pdf->setPrintFooter(false);
// set default monospaced font 设置默认的字体
$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
// set margins
$pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
// set auto page breaks  自动分页
$pdf->SetAutoPageBreak(true, PDF_MARGIN_BOTTOM);
// set image scale factor
$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
foreach ($list as $res) {
    // set font
    //  $pdf->SetFont('stsongstdlight', 'BI', 15);
    // add a page
    $pdf->AddPage();
    // print a block of text using Write()
    $pdf->SetFont('stsongstdlight', 'B', 20);
    $pdf->Write(0, '', '', 0, 'C', true, 0, false, false, 0);
    $pdf->SetFont('stsongstdlight', 'B', 18);
    $pdf->Write(0, '', '', '', 'L', true, 0, false, false, 0);
    $pdf->SetFont('stsongstdlight', '', 13);
    $pdf->Cell(100, 10, ":{$res['name']}", 1, 0, 'L');
    $pdf->Cell(80, 10, ":{$res['no']}", 1, 0, 'L');
    $pdf->Ln();
    $pdf->SetFont('stsongstdlight', '', 13);
    $pdf->Cell(180, 10, "{$res['address']}", 1, 0, 'L');
    $pdf->Ln();
    $pdf->SetFont('stsongstdlight', 'B', 18);
    $pdf->Write(10, '', '', '', 'L', true, 10, true, true, 10);
    $pdf->setColor('fill', 211, 211, 211);
    $pdf->SetFont('stsongstdlight', '', 15);
    $pdf->Cell(90, 10, ":{$res['time']}", 1, 0, 'L');
    $pdf->Cell(90, 10, ":{$res['operator']}", 1, 0, 'L');
    $pdf->Ln();
    $pdf->SetFont('stsongstdlight', '', 15);
    $pdf->Cell(15, 10, '', 1, 0, 'C');
    $pdf->Cell(50, 10, '', 1, 0, 'C');
    $pdf->Cell(40, 10, '', 1, 0, 'C');
    $pdf->Cell(75, 10, '', 1, 0, 'C');
    $pdf->Ln();
    $index = 1;
    $x     = 0;
    $j     = 97;
    $page  = $pdf->getPage();
    foreach ($res['list'] as $item) {
        $pdf->SetFont('stsongstdlight', '', 12);
        $pdf->Cell(15, 40, $index, 1, 0, 'C');
        $pdf->Cell(50, 40, $item['name'], 1, 0, 'C');
        $item['result'] = $item['result'] == 1 ? '合格' : '不合格';
        $pdf->Cell(40, 40, $item['result'], 1, 0, 'C');
        $currentPage = $pdf->getPage();
        if ($page != $currentPage) {
            $height = 33;
            $j      = 30;
            $x      = 1;
            $page   = $currentPage;
        } else {
            $height = $j + 40 * $x;
            $x++;
        }
        $pdf->Cell(75, 40, '', 1, 0, 'C');
        if (!empty($item['photo'])) {
            $pdf->Image($item['photo'], 135, $height, 30, 30, '', '', 'R', false);
        }
        //  $pdf->Cell(75, 10, $item['result'], 1, 0, 'C');
        $index++;
        $pdf->Ln();
    }
    $pdf->Cell(180, 10, ":{$res['advise']}", 1, 0, 'L');
}
$name     = makeOrderNo().'.pdf';
$fileName = APP_UPLOAD_DIR.'/pdf/'.$name;
// ---------------------------------------------------------
//   $pdf->Image('/data/project/hyperf-skeleton/img.png');
$pdf->Output($fileName, 'F');
header('Content-Type:application/force-download');
header('Content-Disposition: attachment;filename="'.$name.'"');
readfile($fileName);
unlink($fileName);
参考链接 https://cloud.tencent.com/developer/article/1488576?from=15425
参考链接 https://www.cnblogs.com/huangguojin/p/16048765.html

 

posted @ 2022-08-15 15:28  西瓜霜  阅读(331)  评论(0编辑  收藏  举报