我的概念是 - 网站上有10个pdf文件.用户可以选择一些pdf文件,然后选择合并以创建包含所选页面的单个pdf文件.我怎么能用PHP做到这一点?
San*_*han 106
下面是php PDF merge命令.
$fileArray= array("name1.pdf","name2.pdf","name3.pdf","name4.pdf");
$datadir = "save_path/";
$outputName = $datadir."merged.pdf";
$cmd = "gs -q -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -sOutputFile=$outputName ";
//Add each pdf file to the end of the command
foreach($fileArray as $file) {
$cmd .= $file." ";
}
$result = shell_exec($cmd);
Run Code Online (Sandbox Code Playgroud)
我忘记了找到它的链接,但它工作正常.
Age*_*nce 34
我建议PDFMerger从github.com,所以容易像::
include 'PDFMerger.php';
$pdf = new PDFMerger;
$pdf->addPDF('samplepdfs/one.pdf', '1, 3, 4')
->addPDF('samplepdfs/two.pdf', '1-2')
->addPDF('samplepdfs/three.pdf', 'all')
->merge('file', 'samplepdfs/TEST2.pdf'); // REPLACE 'file' WITH 'browser', 'download', 'string', or 'file' for output options
Run Code Online (Sandbox Code Playgroud)
Chr*_*sta 24
我之前做过这个.我有一个用fpdf生成的pdf,我需要为它添加可变数量的PDF.
所以我已经有了一个fpdf对象和页面设置(http://www.fpdf.org/)我用fpdi来导入文件(http://www.setasign.de/products/pdf-php-solutions/ fpdi /)通过扩展PDF类添加FDPI:
class PDF extends FPDI
{
}
$pdffile = "Filename.pdf";
$pagecount = $pdf->setSourceFile($pdffile);
for($i=0; $i<$pagecount; $i++){
$pdf->AddPage();
$tplidx = $pdf->importPage($i+1, '/MediaBox');
$pdf->useTemplate($tplidx, 10, 10, 200);
}
Run Code Online (Sandbox Code Playgroud)
这基本上使每个pdf成为一个图像,以放入您的其他PDF格式.它非常适合我需要的东西.
小智 11
$cmd = "gs -q -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -sOutputFile=".$new." ".implode(" ", $files);
shell_exec($cmd);
Run Code Online (Sandbox Code Playgroud)
Chauhan答案的简化版本
既被接受的答案,甚至FDPI主页似乎都给出了拙劣的或不完整的示例。这是可行且易于实现的矿井。不出所料,它需要fpdf和fpdi库:
require('fpdf.php');
require('fpdi.php');
$files = ['doc1.pdf', 'doc2.pdf', 'doc3.pdf'];
$pdf = new FPDI();
// iterate over array of files and merge
foreach ($files as $file) {
$pageCount = $pdf->setSourceFile($file);
for ($i = 0; $i < $pageCount; $i++) {
$tpl = $pdf->importPage($i + 1, '/MediaBox');
$pdf->addPage();
$pdf->useTemplate($tpl);
}
}
// output the pdf as a file (http://www.fpdf.org/en/doc/output.htm)
$pdf->Output('F','merged.pdf');
Run Code Online (Sandbox Code Playgroud)
小智 5
我的软件中有类似的问题。我们希望将多个PDF文件合并为一个PDF文件,然后将其提交给外部服务。我们一直使用Christa所示的FPDI解决方案方案。
但是,我们一直在使用的输入PDF版本可能高于1.7。我们已经决定评估FPDI商业插件。但是,事实证明,我们办公室复印机扫描的某些文档的索引格式错误,这使FPDI商业附加组件崩溃了。因此,我们决定在Chauhan的答案中使用Ghostscript解决方案。
但是,然后在输出PDF属性中得到了一些奇怪的元数据。
最后,我们决定加入两种解决方案,以通过Ghostscript合并和降级PDF,但是元数据由FPDI设置。我们尚不知道它将如何与某些高级格式的pdf一起使用,但对于扫描,我们使用它就可以了。这是我们的课堂摘录:
class MergedPDF extends \FPDI
{
private $documentsPaths = array();
public function Render()
{
$outputFileName = tempnam(sys_get_temp_dir(), 'merged');
// merge files and save resulting file as PDF version 1.4 for FPDI compatibility
$cmd = "/usr/bin/gs -q -dNOPAUSE -dBATCH -dCompatibilityLevel=1.4 -sDEVICE=pdfwrite -sOutputFile=$outputFileName";
foreach ($this->getDocumentsPaths() as $pdfpath) {
$cmd .= " $pdfpath ";
}
$result = shell_exec($cmd);
$this->SetCreator('Your Software Name');
$this->setPrintHeader(false);
$numPages = $this->setSourceFile($outputFileName);
for ($i = 1; $i <= $numPages; $i++) {
$tplIdx = $this->importPage($i);
$this->AddPage();
$this->useTemplate($tplIdx);
}
unlink($outputFileName);
$content = $this->Output(null, 'S');
return $content;
}
public function getDocumentsPaths()
{
return $this->documentsPaths;
}
public function setDocumentsPaths($documentsPaths)
{
$this->documentsPaths = $documentsPaths;
}
public function addDocumentPath($documentPath)
{
$this->documentsPaths[] = $documentPath;
}
}
Run Code Online (Sandbox Code Playgroud)
该类的用法如下:
$pdf = new MergedPDF();
$pdf->setTitle($pdfTitle);
$pdf->addDocumentPath($absolutePath1);
$pdf->addDocumentPath($absolutePath2);
$pdf->addDocumentPath($absolutePath3);
$tempFileName = tempnam(sys_get_temp_dir(), 'merged');
$content = $pdf->Render();
file_put_contents($tempFileName, $content);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
144391 次 |
| 最近记录: |