dongzhanyan3667 2018-06-18 16:17
浏览 116

一个PDF文件中的多个图像

Searching for mPdf Code Example to convert multiple images into a single Pdf. but I failed. Anyone having any code example or link to read please suggest. Thanks in advance.

    <?php
require_once("mpdf60/mpdf.php");
$imageStorage='';
$p1=$_FILES['p1']['name'];
$p1Temp=$_FILES['p1']['tmp_name'];
$p2=$_FILES['p2']['name'];
$p2Temp=$_FILES['p2']['tmp_name'];
$p3=$_FILES['p3']['name'];
$p3Temp=$_FILES['p3']['tmp_name'];
$p4=$_FILES['p4']['name'];
$p4Temp=$_FILES['p4']['tmp_name'];
$imageStorage=$_FILES['p1']['tmp_name'],$_FILES['p2']['tmp_name'],$_FILES['p3']['tmp_name'];,$_FILES['p4']['tmp_name'];

class PDF extends MPDF {
  const DPI = 96;
  const MM_IN_INCH = 25.4;
  const A4_HEIGHT = 297;
  const A4_WIDTH = 210;
// tweak these values (in pixels)
  const MAX_WIDTH = 800;
  const MAX_HEIGHT = 500;
  function pixelsToMM($val) {
    return $val * self::MM_IN_INCH / self::DPI;
  }
  function resizeToFit($imgFilename) {
    list($width, $height) = getimagesize($imgFilename);
    $widthScale = self::MAX_WIDTH / $width;
    $heightScale = self::MAX_HEIGHT / $height;
    $scale = min($widthScale, $heightScale);
    return array(
      round($this->pixelsToMM($scale * $width)),
      round($this->pixelsToMM($scale * $height))
    );
  }
  function centreImage($img) {
    list($width, $height) = $this->resizeToFit($img);
    $this->Image(
      $img, (self::A4_HEIGHT - $width) / 2,
      (self::A4_WIDTH - $height) / 2,
      $width,
      $height
    );
  }
}


$pdf = new PDF();
$pdf->AddPage("L");
$pdf->centreImage($imageStorage);
$pdf->Output();

?>

Here is the code i tried.But Failed

  • 写回答

1条回答 默认 最新

  • doob0526 2018-06-20 09:31
    关注

    This line of your code is completely wrong:

    $imageStorage=$_FILES['p1']['tmp_name'],$_FILES['p2']['tmp_name'],$_FILES['p3']['tmp_name'];,$_FILES['p4']['tmp_name'];
    

    If you want to get uploaded images, you can format your html like below:

    Generatepdf.php

    <form method="post" action="uploaded.php" enctype="multipart/form-data">
        <input type="file" name="files[]" multiple>
        <input type="submit" name="submit" value="Upload">
    </form>
    

    With the above html, you can upload multiple images. And you get the uploaded images using the following php code:

    Uploaded.php

    require_once("mpdf.php");//
    $uploaded = [];//New empty array for holding uploaded images
    foreach ($_FILES['files']['tmp_name'] as $key => $tmp_name) {
      $uploaded[] = file_get_contents($_FILES['files']['tmp_name'][$key]);
    }
    

    As the MPDF image() method does not accept image resource, I used a quick work around with GD image functions. So your new PDF class:

    class PDF extends MPDF {
      const DPI = 96;
      const MM_IN_INCH = 25.4;
      const A4_HEIGHT = 297;
      const A4_WIDTH = 210;
    // tweak these values (in pixels)
      const MAX_WIDTH = 800;
      const MAX_HEIGHT = 500;
      function pixelsToMM($val) {
        return $val * self::MM_IN_INCH / self::DPI;
      }
      function resizeToFit($imgFilename) {
        list($width, $height) = getimagesizefromstring($imgFilename);
        $widthScale = self::MAX_WIDTH / $width;
        $heightScale = self::MAX_HEIGHT / $height;
        $scale = min($widthScale, $heightScale);
        return array(
          round($this->pixelsToMM($scale * $width)),
          round($this->pixelsToMM($scale * $height))
        );
      }
      function centreImage($imgdata) {
        list($width, $height) = getimagesizefromstring($imgdata);//Old size
        list($newwidth, $newheight) = $this->resizeToFit($imgdata);//new size
        $thumb = imagecreatetruecolor($newwidth, $newheight);
        $source = imagecreatefromstring($imgdata);
        imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);  
        ob_start(); // Let's start output buffering.
        imagejpeg($thumb); //This will normally output the image, but because of ob_start(), it won't.
        $contents = ob_get_contents(); //Instead, output above is saved to $contents
        ob_end_clean(); //End the output buffer.    
        return $contents;
      }
    }
    

    The code to generate images on one PDF:

    $pdf = new PDF();
    $pdf->AddPage("L");
    $pdf->SetColumns(4,2);//Four columns, 2mm gap between the columns
    $pdf->max_colH_correction = 10;
    foreach($uploaded as $changefile){
      $pdf->WriteHTML("<img src='data:image/jpeg;base64,".base64_encode($pdf->centreImage($changefile))."' />");
    }  
    $pdf->Output('filename.pdf','F');
    

    I don't have much time to play with MPDF image() method. You can visit the link and see what you can play with. I hope this will help you and give you something to work with GD library as well.

    评论

报告相同问题?

悬赏问题

  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应