如何将 PHP 中的图像插入 PDF 1.7
How to insert an image from PHP into PDF 1.7
我正在创建一个 Web 应用程序,它允许 canvas 表单将 HTML canvas 中的图像插入到多个 PDF 文件的特定位置。我用 python flask 作为后端来工作,但我为之制作它的人只希望在 PHP 中使用它。我曾尝试使用像 FPDI 这样的库,但它们只适用于 1.4 以下的 PDF 版本,而我们使用的 PDF 文件是 1.7 版。
有谁知道任何可能的库可以帮助我解决这个问题。如果可能的话,我不想转换 PDF 文件。
干杯
使用 TCPDF,您可以将图像插入 PDF (v.1.7) 文件:
要求
composer require tecnickcom/tcpdf
示例
<?php
require_once __DIR__ . '/vendor/autoload.php';
$pdf = new TCPDF();
$pdf->setPDFVersion('1.7');
$pdf->setAutoPageBreak(true);
$pdf->setPrintHeader(false);
$pdf->setPrintFooter(false);
$pdf->AddPage();
// Insert image
$pdf->setJPEGQuality(100);
$pdf->image(__DIR__ . '/example.jpg', 10, 10);
// Close and output PDF document
//$pdf->output('doc.pdf', 'I');
// Save the pdf file
$content = $pdf->output('', 'S');
file_put_contents('example.pdf', $content);
我们(Setasign,FPDI 的创建者)提供商业 add-on,让您导入 PDF,它使用 PDF 1.5 中引入的压缩技术。
您也可以尝试使用外部程序降级这些文档。我知道有些人为此使用 Ghostscript。
通常您应该知道,您不会将图像插入现有的 PDF 中,而是创建一个全新的 PDF,同时将单个页面导入到可重复使用的结构中,然后将其放置到新创建的页面上。在此之上放置图像。
我正在创建一个 Web 应用程序,它允许 canvas 表单将 HTML canvas 中的图像插入到多个 PDF 文件的特定位置。我用 python flask 作为后端来工作,但我为之制作它的人只希望在 PHP 中使用它。我曾尝试使用像 FPDI 这样的库,但它们只适用于 1.4 以下的 PDF 版本,而我们使用的 PDF 文件是 1.7 版。
有谁知道任何可能的库可以帮助我解决这个问题。如果可能的话,我不想转换 PDF 文件。
干杯
使用 TCPDF,您可以将图像插入 PDF (v.1.7) 文件:
要求
composer require tecnickcom/tcpdf
示例
<?php
require_once __DIR__ . '/vendor/autoload.php';
$pdf = new TCPDF();
$pdf->setPDFVersion('1.7');
$pdf->setAutoPageBreak(true);
$pdf->setPrintHeader(false);
$pdf->setPrintFooter(false);
$pdf->AddPage();
// Insert image
$pdf->setJPEGQuality(100);
$pdf->image(__DIR__ . '/example.jpg', 10, 10);
// Close and output PDF document
//$pdf->output('doc.pdf', 'I');
// Save the pdf file
$content = $pdf->output('', 'S');
file_put_contents('example.pdf', $content);
我们(Setasign,FPDI 的创建者)提供商业 add-on,让您导入 PDF,它使用 PDF 1.5 中引入的压缩技术。
您也可以尝试使用外部程序降级这些文档。我知道有些人为此使用 Ghostscript。
通常您应该知道,您不会将图像插入现有的 PDF 中,而是创建一个全新的 PDF,同时将单个页面导入到可重复使用的结构中,然后将其放置到新创建的页面上。在此之上放置图像。