thinkphp || 预览导出pdf

composer require tecnickcom/tcpdf

/**

 * setPdf 输出PDF

 * @param string $content 文件内容

 * @param array $title    文件名称

 * @author Mr.Lv   3063306168@qq.com

 */

function setPdf($content, $title){

    // $logo    = Env::get('root_path') .'public/static/images/logo.jpg';

    // require Env::get('root_path') .'vendor/tecnickcom/tcpdf/tcpdf.php';

    $pdf  = new \TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);

    $pdf->SetCreator(PDF_CREATOR);

    $pdf->SetAuthor("作者");//设置作者

    $pdf->SetTitle($title);

    $pdf->SetSubject('TCPDF Tutorial');

    //$pdf->SetKeywords('TCPDF, PDF, example, test, guide');//设置关键字

    // 是否显示页眉

    $pdf->setPrintHeader(false);

    // 设置页眉显示的内容

    //$pdf->SetHeaderData($logo, 60, '', '');

    // 设置页眉字体

    //$pdf->setHeaderFont(Array('deja2vusans', '', '12'));

    // 页眉距离顶部的距离

    $pdf->SetHeaderMargin('5');

    // 是否显示页脚

    //$pdf->setPrintFooter(true);

    // 设置页脚显示的内容

    //$pdf->setFooterData(array(0,64,0), array(0,64,128));

    // 设置页脚的字体

    //$pdf->setFooterFont(Array('dejavusans', '', '10'));

    // 设置页脚距离底部的距离

    //$pdf->SetFooterMargin('10');

    // 设置默认等宽字体

    $pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);

    // 设置行高

    $pdf->setCellHeightRatio(1.5);

    // 设置左、上、右的间距

    $pdf->SetMargins('15', '15', '15');

    // set auto page breaks

    //设置是否自动分页  距离底部多少距离时分页

    $pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);

    // 设置字体

    $pdf->SetFont('stsongstdlight', '', 14, '', true);

    // 设置是否自动分页 距离底部多少距离时分页

    //$pdf->SetAutoPageBreak(TRUE, '15');

    // 设置图像比例因子

    $pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);

    //$pdf->setFontSubsetting(true);

    $pdf->AddPage("A4","Landscape",true,true);

    //$pdf->writeHTML($content);//HTML生成PDF

    $pdf->writeHTML('<img width="95"><br>'.$content, true, false, true, false, '');//设置logo

    //生成PDF文件到某地
    //$pdf->writeHTMLCell(0, 0, '', '', $content, 0, 1, 0, true, '', true);

    $showType= 'I';//PDF输出的方式。I,在浏览器中打开;D,以文件形式下载;F,保存到服务器中;S,以字符串形式输出;E:以邮件的附件输出。

    $pdf->Output("pdf.pdf", $showType);

    exit();

}

调用:

   public function download()
    {
        $content = '你好啊';   
        $title = 'pdf导出';
    	setPdf($content,$title);
    }

在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值