PHP FPDF将图像附加到现有PDF文件的末尾

时间:2018-04-17 12:50:40

标签: php fpdf fpdi

我有一个PHP应用程序,我必须从桌面浏览图像并将其添加到保存在项目文件中的现有PDF文件中。

现在,我的PDF文件已有大约5-6张图像,我需要在PDF文件的末尾添加此图像。

我也不知道如何让图像适合整个PDF文件页面,这就是我将其参数设置为:

的原因
      $pdf->Image($imagePath,0,0); //It is wrong I guess

这是我迄今为止使用Stack Overflow帮助的代码,但它根本不起作用,我收到了一些错误。

 <?php 
  use setasign\Fpdi\Fpdi;  //I just pasted this line not knowing how it works
  include 'header.php'; 
  require_once 'fpdf181/fpdf.php';
  require_once 'FPDI-2/src/autoload.php';
  require_once 'FPDI-2/src/Fpdi.php';
?>

<html>
<head>
       <script>
               var x; //stores image path, which is somewhat working
               function getImagePath()
               {
                  x = document.getElementById("myFile").files[0].name;
                  alert(x);
               }
      </script>
<head>

<body>
    <div id="container">

        //HTML Button to browse for an image
        <input type="file" id="myFile" size="50">

        //HTML Button to display image file path
        <button type = "button" onclick="getImagePath()">Check path</button>

        <?php 
            // initiate FPDI
            $pdf = new FPDI();

            // get the page count
            $pageCount = $pdf->setSourceFile('fpdf_merge/ex.pdf');
            // iterate through all pages
            for ($pageNo = 1; $pageNo <= $pageCount; $pageNo++) 
            {
                // import a page
                $templateId = $pdf->importPage($pageNo);
                // get the size of the imported page
                $size = $pdf->getTemplateSize($templateId);

                // create a page (landscape or portrait depending on the imported page size)
                if ($size['w'] > $size['h']) 
                    {
                        $pdf->AddPage('L', array($size['w'], $size['h']));
                    } 
                else 
                    {
                        $pdf->AddPage('P', array($size['w'], $size['h']));
                    }

                // use the imported page
                $pdf->useTemplate($templateId);
            }

            //this is where the error is occurring i.e Uncaught Exception: FPDF error: Unsupported image type: write(x)</script>

            $imagePath = "<script>document.write(x)</script>"; 


            //the second error from below code: FPDF->Error('Unsupported ima...') #1 

            $pdf->Image($imagePath,0,0);

            // Output the new PDF
            $pdf->Output();  //Does this step save changes to the exisiting pdf?
        ?>
    </div>
</body>

从JavaScript变量

中读取图像路径时发生错误
Uncaught Exception: FPDF error: Unsupported image type: write(x)</script>

将图像添加到pdf

时发生第二个错误
FPDF->Error('Unsupported ima...') #1

我不确定如何解决这个问题。

0 个答案:

没有答案