PHP Tcpdf 生成 pdf 并将 (Imagick) pdf 生成 图片

利用Tcpdf生成pdf,使用 html标签,生成 pdf

一、 拉取扩展代码
使用 composer 拉取

composer require tecnickcom/tcpdf

https://github.com/tecnickcom/tcpdf 直接下载

官网地址 : http://www.tcpdf.org

如下效果:在这里插入图片描述

二、开发代码 html 生成pdf
html样式格式可以参考官网案例 https://tcpdf.org/examples/

require_once(dirname(dirname(__FILE__)) . '/sdk/tcpdf.php'); //记得引入

 public function createPdf($pdf_name,$data=[],$type=1)
    {
        if(empty($pdf_name)){
            $pdf_name = md5(microtime());
        }
        $pdf_name = $pdf_name;
        $pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT,true, 'UTF-8', false);
		// 设置文档信息
		$pdf->SetCreator('hello world');
		$pdf->SetAuthor('hello world');
		$pdf->SetTitle('hello world');
		$pdf->SetSubject('TCPDF Tutorial');
		$pdf->SetKeywords('TCPDF, PDF, PHP');
		// 设置页眉和页脚信息
//		$pdf->SetHeaderData('logo.png', 30, 'Helloweba.com', '致力于WEB前端技术在中国的应用', array(0,64,255), array(0,64,128));
		$pdf->setFooterData(array(0,64,0), array(0,64,128));
		// 设置页眉和页脚字体
		$pdf->setHeaderFont(Array('stsongstdlight', '', '10'));
		$pdf->setFooterFont(Array('helvetica', '', '8'));
		// 设置默认等宽字体
		$pdf->SetDefaultMonospacedFont('courier');
		// 设置间距
		$pdf->SetMargins(15, 27, 15);
		$pdf->SetHeaderMargin(5);
		$pdf->SetFooterMargin(10);
		// 设置分页
		$pdf->SetAutoPageBreak(TRUE, 25);
		// set image scale factor
		$pdf->setImageScale(1.25);
		// set default font subsetting mode
		$pdf->setFontSubsetting(true);
		//设置字体
		$pdf->SetFont('stsongstdlight', '', 14);//可以设置自己中意字体
		$pdf->AddPage();
		$str1 = $this->getHtml($pdf_name,$data,$pdf);
        $pdf->writeHTML($str1, true, false, true, false, '');

        //输出PDF
        ob_clean();
        $pdf_path = ROOT_PATH .'public/pdf/'.$pdf_name.'.pdf';
        $pdf_png_path = ROOT_PATH .'publi/pdf/png/'.$pdf_name.'.png';
       
      
        $pdf->Output($pdf_path, 'F'); //I:将文件内联发送到浏览器(默认)。如果可用,则使用插件。当在生成PDF的链接上选择“另存为”选项时,使用name指定的名称。D:发送到浏览器并强制使用name指定的名称下载文件。F: 以指定的名称保存到本地服务器文件。S:以字符串形式返回文档(忽略名称)。FI:等效于F + I选项FD:等效于F + D选项E:返回文档为base64 mime多部分电子邮件附件(RFC 2045) 

        $this->pdf2png($pdf_path,$pdf_png_path);
        return array($pdf_url,$pdf_png_url);

    }

//获取html样式
public funcitoin getHtml(){
	$html = '<h1>HTML Example</h1>
	Some special characters: &lt; € &euro; &#8364; &amp; è &egrave; &copy; &gt; \\slash \\\\double-slash \\\\\\triple-slash
	<h2>List</h2>
	List example:
	<ol>
	    <li><img src="images/logo_example.png" alt="test alt attribute" width="30" height="30" border="0" /> test image</li>
	    <li><b>bold text</b></li>
	    <li><i>italic text</i></li>
	    <li><u>underlined text</u></li>
	    <li><b>b<i>bi<u>biu</u>bi</i>b</b></li>
	    <li><a href="http://www.tecnick.com" dir="ltr">link to http://www.tecnick.com</a></li>
	    <li>Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo.<br />Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt.</li>
	    <li>SUBLIST
	        <ol>
	            <li>row one
	                <ul>
	                    <li>sublist</li>
	                </ul>
	            </li>
	            <li>row two</li>
	        </ol>
	    </li>
	    <li><b>T</b>E<i>S</i><u>T</u> <del>line through</del></li>
	    <li><font size="+3">font + 3</font></li>
	    <li><small>small text</small> normal <small>small text</small> normal <sub>subscript</sub> normal <sup>superscript</sup> normal</li>
	</ol>
	<dl>
	    <dt>Coffee</dt>
	    <dd>Black hot drink</dd>
	    <dt>Milk</dt>
	    <dd>White cold drink</dd>
	</dl>
	<div style="text-align:center">IMAGES<br />
	<img src="images/logo_example.png" alt="test alt attribute" width="100" height="100" border="0" /><img src="images/tcpdf_box.svg" alt="test alt attribute" width="100" height="100" border="0" /><img src="images/logo_example.jpg" alt="test alt attribute" width="100" height="100" border="0" />
	</div>';
	return $html;

}


也可以使用其他形式,生成 pdf,有兴趣的同学可以尝试一下
在这里插入图片描述

三、使用 Imagick 将 pdf 生成 png
根据介绍安装一下php扩展 https://www.php.net/manual/zh/book.imagick.php

在这里插入图片描述

 //pdf合并转换一张图片 from_path pdf来源地址(绝对地址) target_path 图片存储地址 (绝对地址)
    public  function pdf2png($from_path,$target_path)
    {
 
        try {
            $img =  new \Imagick();
            $img->setCompressionQuality(100);
            $img->setResolution(120, 120);
            $img->readImage($from_path);

            $canvas = new \Imagick();
            $imgNum = $img->getNumberImages();
            foreach ($img as $k => $sub) {
                $sub->setImageFormat('png');
                $sub->stripImage();
                $sub->trimImage(0);
                $width = $sub->getImageWidth() + 10;
                $height = $sub->getImageHeight() + 10;
                if ($k + 1 == $imgNum) $height += 10;
                $canvas->newImage($width, $height, new \ImagickPixel('white'));
                $canvas->compositeImage($sub, \Imagick::COMPOSITE_DEFAULT, 5, 5);
            }

            $canvas->resetIterator();
            $canvas->appendImages(true)->writeImage($target_path);
            return $target_path;
         } catch (Exception $e) {
            echo $e->getMessage();
            echo $e->getTraceAsString();
            return false;
        }
    }

四、注意事项

1、使用 Tcpdf html生成 pdf时,注意一下html的样式问题,不支持外部css样式

2、使用 Tcpdf html生成 pdf时,注意一下html的样式问题, 你看到html不一定能生成正常html哦!

3、Imagick 安装时,一定要注意检查是否安装成功,否则会有报错信息,不能正常使用

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值