• 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




  • 相关阅读:
    协程,纤程(Fiber),或者绿色线程(GreenThread)
    好用的 Chrome 插件
    内存泄露
    Serilog 是 ASP.NET Core 的一个插件,可以简化日志记录
    ES6-类(Class)
    规范-Git打标签与版本控制
    必会必知git
    Ubuntu 16.04安装CrossOver容器来安装QQ(终极解决办法,亲测有效)
    Ubuntu 16.04安装UML工具StarUML 2
    Ubuntu 16.04升级4.7.0内核后导致Compiz奔溃,问题:compiz[4852]: segfault at 48 ip 00007f88cae087f0 sp 00007ffce354c268 error 4 in libscale.so
  • 原文地址:https://www.cnblogs.com/huangguojin/p/16048765.html
Copyright © 2020-2023  润新知