网站首页 文章专栏 php图片转pdf&图片转pdf上传七牛云(使用laravel框架)
php图片转pdf&图片转pdf上传七牛云(使用laravel框架)
编辑时间:2024-06-12 14:55:24 作者:史亚运 浏览量:0

安装fpdf依赖

composer require setasign/fpdf

代码实现

<?phpnamespace App\Utils;
use Qiniu\Auth;
use Qiniu\Storage\UploadManager;
class Tools{
	/**
	图片转成PDF保存在本地
	@author jason
	*/
	public static function imageToPdf()
	{
		$imagePath = public_path() . '/label.png';
		// 获取原始PNG图片的宽度和高度  
		list($imageWidth, $imageHeight) = getimagesize($imagePath);
		//创建FPDF实例
        $pdf = new \FPDF();
		// 创建一页PDF,设置页面大小与图片大小相同  
		$pdf->AddPage();  
		$pdf->SetPageSize(array($imageWidth, $imageHeight));  
		  
		// 将PNG图片添加到PDF中  
		$pdf->Image($imagePath, 0, 0, $imageWidth, $imageHeight);  
		  
		// 保存PDF文件  
		$pdf->Output(public_path() . '/label.pdf', 'F');
	}

	/**
	图片转成PDF并上传七牛云
	@author jason
	*/
	public static function uploadAsPdf()
	{
		$imagePath = public_path() . '/label.png';
		/*转换成PDF*/
        //创建FPDF实例
        $pdf = new \FPDF();
        // 创建一页PDF
        //$pdf->AddPage('', 'A4');//默认页面A4
        $pdf->AddPage();
        // 将PNG图片添加到PDF中
        $pdf->Image($imagePath, 0, 0, 210, 346);
        // 将PDF输出到字符串
        ob_start();
        $pdf->Output();
        $pdfContent = ob_get_clean();

        // 构建鉴权对象(配置文件中获取密钥)
        $config = config('filesystems.disks');
        $accessKey = $config['qiniu']['access_key'];
        $secretKey = $config['qiniu']['secret_key'];
        $bucket = $config['qiniu']['bucket'];
        $domain = $config['qiniu']['domains']['custom'];
        $auth = new Auth($accessKey, $secretKey);
        $token = $auth->uploadToken($bucket);
        $uploadMgr = new UploadManager();
        // 文件名
        $key = 'label.pdf';
        list($ret, $err) = $uploadMgr->put($token, $key, $pdfContent);
        if ($err !== null) {
        	throw new \Exception($err);
			return false;
		}
        return $ret;
	}}


来说两句吧
最新评论