• Yii 结合MPDF 给PDF文件添加多行水印


    首先确保安装了mpdf扩展 composer require mpdf/mpdf

    1. public function createWaterPdf($file_path,$water_text)
    2. {
    3. date_default_timezone_set('PRC');
    4. ini_set('memory_limit', '6400M');
    5. ini_set('max_execution_time', '0');
    6. try{
    7. $mpdf = new Mpdf();
    8. $pageCount = $mpdf->SetSourceFile($file_path); //读取原始文件页数
    9. $tplId = $mpdf->ImportPage(1); //获取第一张信息
    10. $size = $mpdf->getTemplateSize($tplId);
    11. $orientation = $size['orientation'] ?? 'P';
    12. //创建缓存文件夹
    13. $cachePath = Yii::$app->basePath.'/runtime/pdfwater';
    14. if (!is_dir($cachePath)) {
    15. mkdir($cachePath, 0777, true);
    16. }
    17. //第一步:创建水印背景图片
    18. $width = ceil($size['width'] / 25.4 * 300); //换算成300分辨率对应的尺寸大小
    19. $height = ceil($size['height'] / 25.4 * 300);
    20. $image = imagecreate($width, $height);
    21. $bg = imagecolorallocate($image, 255, 255, 255);
    22. imagefill($image, 0, 0, $bg);
    23. imagepng($image,$cachePath.'/background.png');
    24. imagedestroy($image);
    25. //第二步:创建水印图片
    26. $image = $cachePath . '/background.png';
    27. $info = getimagesize($image);
    28. $imgExt = image_type_to_extension($info[2], false); //获取文件后缀
    29. $fun = "imagecreatefrom{$imgExt}";
    30. $imgInfo = $fun($image);
    31. $fontFile = Yii::$app->params['fontPath'] . '/simhei.ttf'; //字体文件名,必须要,需要下载对应的字体文件,放到指定的目录
    32. $fontSize = 70; //字体尺寸
    33. $fontColor = ImageColorAllocate($imgInfo,0,0,0); //字体颜色,这里是黑色
    34. $textAngle = 30; //文字显示的角度,0表示水平显示
    35. $x_length = $info[0];
    36. $y_length = $info[1];
    37. $text_length = (mb_strlen($water_text) + 3)*0.833*70;
    38. //判断用多大的纸张
    39. if ($orientation == 'P') {
    40. $water_h = ceil($x_length/3)-100;
    41. $page_width = $size[0];
    42. $page_height = $size[1];
    43. } else {
    44. $water_h = ceil($y_length/3)-100;
    45. $page_width = $size[1];
    46. $page_height = $size[0];
    47. }
    48. if ($page_width > 900 || $page_width < 150 || $page_height > 1200 || $page_height < 150) return ['code'=>2,'msg'=>'文件尺寸不符合要求']; //此处不限制文件大小也行
    49. $water_w = $text_length * 1.732;
    50. if ($water_w < 1000) $water_w = 1000;
    51. //重复添加水印
    52. for ($x = 0; $x < $x_length; $x += $water_w) {
    53. for ($y = 0; $y < $y_length; $y += $water_h) {
    54. imagefttext($imgInfo,$fontSize,$textAngle,$x,$y,$fontColor,realpath($fontFile),$water_text); //把文字覆盖到图片上
    55. }
    56. }
    57. $quality = 100;
    58. if($imgExt == 'png') $quality = 9;//输出质量,JPEG格式(0-100),PNG格式(0-9)
    59. $getImgInfo = "image{$imgExt}";
    60. $getImgInfo($imgInfo, $cachePath.'/watermark.png', $quality);
    61. imagedestroy($imgInfo);
    62. //第三步:开始添加水印操作
    63. $arrPage = [
    64. 'sheet-size' => [$page_width,$page_height], //指定纸张大小
    65. 'orientation' => $orientation, //指定纸张方向
    66. ];
    67. for($i=1; $i<=$pageCount; $i++){ //循环添加原始文件
    68. $mpdf->AddPageByArray($arrPage);
    69. $tplId = $mpdf->ImportPage($i);
    70. $mpdf->UseTemplate($tplId);
    71. //开始给pdf添加水印
    72. $mpdf->SetWatermarkImage($cachePath.'/watermark.png');
    73. $mpdf->showWatermarkImage = true;
    74. }
    75. $mpdf->Output($cachePath.'/watermask.pdf','F'); //新文件
    76. }catch (\Exception $e){
    77. return ['code'=>1,'msg'=>$e->getMessage()];
    78. }
    79. }

  • 相关阅读:
    Abbkine SuperKine 增强型抗荧光淬灭剂实验建议&特点
    Android进阶:6、使用okhttp下载图片
    【抓包工具】win 10 / win 11:WireShark 下载、安装、使用
    Android平台轻量级RTSP服务模块如何实现一个服务发布多路RTSP流?
    MySQL面试问题汇总(2022)
    aspnet基于mvc松茸进出口特产销售网站
    JDK多版本切换
    Linux环境基础开发工具使用
    [SWPUCTF 2021 新生赛]sql - 联合注入
    VsCode中一些可以让工作“事半功倍”的插件
  • 原文地址:https://blog.csdn.net/m0_38122341/article/details/139269760