首页 > 编程语言 >php 导出pdf

php 导出pdf

时间:2022-08-15 15:34:52浏览次数:55  
标签:10 导出 stsongstdlight Cell PDF SetFont pdf php

  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

 

标签:10,导出,stsongstdlight,Cell,PDF,SetFont,pdf,php
From: https://www.cnblogs.com/zonglonglong/p/16588473.html

相关文章

  • thinkphp6中关于时间的查询方法
    whereTime用法一:查询今天的数据$list=$userModel->whereTime('add_time','today')->select();同理,替换参数today后可以查询昨天(yesterday)、本周(week)、下周(last......
  • java实现word模板导出
    Xdocreport和Freemaker生成docx只使用freemaker生成简单的word文档很容易,但是当word文档需要插入动态图片,带循环数据,且含有富文本时解决起来相对比较复杂,但是使用Xdocrepo......
  • PHP与JS互相加密解密方法2.0【转载】
    本文转自:https://blog.csdn.net/qq_32845825/article/details/123705487前言:之前写过一个加密解密1.0版本的,但是随着PHP版本升级,那个不能用了,当初使用的是PHP5中的mcrypt......
  • php以图形方式显示中文,指定ttf字库
    1<?php2header("Content-Type:image/png");3$img=imagecreatetruecolor(400,300);4//imagejpeg($img);5//imagejpeg($img,"./img/copy_img01.jpg",10);......
  • PDF划词拾取原理
    背景PDF划词拾取在许多业务场景中都有使用,典型的场景例如划词翻译、划词批注、下划线、删除线等等。网上通用做法是用css实现下划线、删除线、高亮。但是这种做法通常不利......
  • 《GB14925-2010》PDF下载
    《GB14925-2010实验动物环境及设施》PDF下载《GB14925-2010》简介本标准规定了实验动物及动物实验设施和环境条件的技术要求及检测方法,同时规定了垫料、饮水和笼具的......