PHP laravel artisan将图片转换成PDF

<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;

class Image extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'image {src_file : 输入路径} {dst_file : 输出路径}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        $src_file = $this->argument('src_file');
        $dst_file = $this->argument('dst_file');

        if(empty($src_file) || empty($dst_file)){
            cmslog(0,['src_file' => $src_file, 'dst_file' => $dst_file, 'error_data' => 0]);
            echo 'save fail: 源文件或目标文件为空';
            exit;
            //return $this->error(__('lang.error_data'));
        }
        //var_dump("睡眠10秒,已处理完成".$this->argument('src_file').'--'.$this->argument('dst_file'));
        try {
            $img_info = getimagesize($src_file);
            $pageWidth = $img_info[0];
            $pageHeight = $img_info[1];

            $pageWidth = $pageWidth / 72 * 25.4;
            $pageHeight = $pageHeight / 72 * 25.4;

            $mpdf = new \Mpdf\Mpdf(['mode' => 'utf-8',"tempDir" => storage_path('tempdir'), 'format' => [$pageWidth, $pageHeight]]);

            $mpdf->WriteHTML("<html><body style='background-image:url(\"". $src_file ."\"); background-image-resize: 5; background-position: top center;'></body></html>");
            //$mpdf->Output();

            $mpdf->Output($dst_file, \Mpdf\Output\Destination::FILE);

        } catch (\Exception $exception) {
            cmslog(0,['src_file' => json_encode($exception->getMessage(),true), 'error_data' => 'Exception']);
            echo "save fail:" . $exception->getMessage();
            exit;
            //return $this->error(__('lang.error_data'));
        }
        //cmslog(0,['src_file' => $src_file, 'dst_file' => $dst_file, 'error_data' => 'success']);
        echo 'save ok';
        exit;
    }
}

/usr/local/php/bin/php /usr/local/apache/htdocs/cms/artisan image '/usr/local/apache/htdocs/webserver/ftp/htmlbanner/108/003.png' '/usr/local/apache/htdocs/webserver/ftp/htmlbanner/108/49.pdf'

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值