Khái niệm của tôi là - có 10 tệp pdf trong một trang web. Người dùng có thể chọn một số tệp pdf và sau đó chọn hợp nhất để tạo một tệp pdf duy nhất chứa các trang đã chọn. Làm thế nào tôi có thể làm điều này với php?
Khái niệm của tôi là - có 10 tệp pdf trong một trang web. Người dùng có thể chọn một số tệp pdf và sau đó chọn hợp nhất để tạo một tệp pdf duy nhất chứa các trang đã chọn. Làm thế nào tôi có thể làm điều này với php?
Câu trả lời:
Tôi đã làm điều này trước đây. Tôi đã có một bản pdf mà tôi đã tạo bằng fpdf và tôi cần thêm một lượng PDF khác nhau vào đó.
Vì vậy, tôi đã thiết lập một đối tượng và trang fpdf (http://www.fpdf.org/) Và tôi đã sử dụng fpdi để nhập các tệp (http://www.setasign.de/products/pdf-php-solutions/ fpdi /) FDPI được thêm vào bằng cách mở rộng lớp PDF:
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);
}
Về cơ bản, điều này làm cho mỗi pdf thành một hình ảnh để đưa vào pdf khác của bạn. Nó hoạt động tốt một cách đáng kinh ngạc cho những gì tôi cần nó.
Dưới đây là lệnh trộn PDF php.
$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);
Tôi quên liên kết từ nơi tôi tìm thấy nó, nhưng nó hoạt động tốt.
Lưu ý: Bạn nên cài đặt gs (trên linux và có thể là Mac) hoặc Ghostscript (trên windows) để tính năng này hoạt động.
tôi đề xuất PDFMerger từ github.com , rất dễ dàng như ::
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
$cmd = "gs -q -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -sOutputFile=".$new." ".implode(" ", $files);
shell_exec($cmd);
Một phiên bản đơn giản của câu trả lời của Chauhan
Cả câu trả lời được chấp nhận và thậm chí cả trang chủ FDPI dường như đều đưa ra các ví dụ sai lệch hoặc không đầy đủ. Đây là của tôi hoạt động và dễ thực hiện. Như mong đợi, nó yêu cầu thư viện fpdf và 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');
Tôi đã gặp vấn đề tương tự trong phần mềm của mình. Chúng tôi muốn hợp nhất nhiều tệp PDF thành một tệp PDF và gửi nó đến một dịch vụ bên ngoài. Chúng tôi đã sử dụng giải pháp FPDI như trong giải pháp của Christa .
Tuy nhiên, tệp PDF đầu vào mà chúng tôi đang sử dụng có thể ở phiên bản cao hơn 1.7. Chúng tôi đã quyết định đánh giá tiện ích bổ sung thương mại FPDI. Tuy nhiên, hóa ra một số tài liệu được quét bởi máy photocopy văn phòng của chúng tôi có chỉ mục không đúng định dạng, điều này đã làm hỏng tiện ích bổ sung FPDI thương mại. Vì vậy, chúng tôi đã quyết định sử dụng giải pháp Ghostscript như trong câu trả lời của Chauhan .
Nhưng sau đó chúng tôi nhận được một số siêu dữ liệu lạ trong các thuộc tính PDF đầu ra.
Cuối cùng, chúng tôi đã quyết định kết hợp hai giải pháp để hợp nhất và hạ cấp PDF bởi Ghostscript, nhưng siêu dữ liệu được đặt bởi FPDI. Chúng tôi vẫn chưa biết nó sẽ hoạt động như thế nào với một số pdf được định dạng nâng cao, nhưng để quét, chúng tôi sử dụng nó hoạt động tốt. Đây là đoạn trích của lớp chúng tôi:
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;
}
}
Cách sử dụng của lớp này như sau:
$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);
$cmd = "\"C:\\Program Files\\gs\\gs9.20\\bin\\gswin64c.exe\" -q -dNOPAUSE -dBATCH -dCompatibilityLevel=1.4 -sDEVICE=pdfwrite -sOutputFile=[....your parameters...]" ;
Tôi đã thử vấn đề tương tự và hoạt động tốt, hãy thử nó. Nó có thể xử lý các hướng khác nhau giữa các tệp PDF.
// array to hold list of PDF files to be merged
$files = array("a.pdf", "b.pdf", "c.pdf");
$pageCount = 0;
// initiate FPDI
$pdf = new FPDI();
// iterate through the files
foreach ($files AS $file) {
// get the page count
$pageCount = $pdf->setSourceFile($file);
// 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);
$pdf->SetFont('Helvetica');
$pdf->SetXY(5, 5);
$pdf->Write(8, 'Generated by FPDI');
}
}
Undefined index: w
Tôi đã tạo một lớp trừu tượng trên FPDI (có thể phù hợp với các công cụ khác). Tôi đã xuất bản nó dưới dạng gói Symfony2 tùy thuộc vào thư viện và như chính thư viện.
sử dụng:
public function handlePdfChanges(Document $document, array $formRawData)
{
$oldPath = $document->getUploadRootDir($this->kernel) . $document->getOldPath();
$newTmpPath = $document->getFile()->getRealPath();
switch ($formRawData['insertOptions']['insertPosition']) {
case PdfInsertType::POSITION_BEGINNING:
// prepend
$newPdf = $this->pdfManager->insert($oldPath, $newTmpPath);
break;
case PdfInsertType::POSITION_END:
// Append
$newPdf = $this->pdfManager->append($oldPath, $newTmpPath);
break;
case PdfInsertType::POSITION_PAGE:
// insert at page n: PdfA={p1; p2; p3}, PdfB={pA; pB; pC}
// insert(PdfA, PdfB, 2) will render {p1; pA; pB; pC; p2; p3}
$newPdf = $this->pdfManager->insert(
$oldPath, $newTmpPath, $formRawData['insertOptions']['pageNumber']
);
break;
case PdfInsertType::POSITION_REPLACE:
// does nothing. overrides old file.
return;
break;
}
$pageCount = $newPdf->getPageCount();
$newPdf->renderFile($mergedPdfPath = "$newTmpPath.merged");
$document->setFile(new File($mergedPdfPath, true));
return $pageCount;
}
Điều này đã làm việc cho tôi trên Windows
thêm phần sau vào mã php của bạn, trong đó $ file1 là vị trí và tên của tệp PDF đầu tiên, $ file2 là vị trí và tên của tệp thứ hai và $ newfile là vị trí và tên của tệp đích
$file1 = ' c:\\\www\\\folder1\\\folder2\\\file1.pdf';
$file2 = ' c:\\\www\\\folder1\\\folder2\\\file2.pdf';
$file3 = ' c:\\\www\\\folder1\\\folder2\\\file3.pdf';
$command = 'cmd /c C:\\\pdftk\\\bin\\\pdftk.exe '.$file1.$file2.$newfile;
$result = exec($command);
$command = "cmd /c C:\\pdftk\\bin\\pdftk.exe {$file1} {$file2} cat output {$new}";
Lưu ý đầu ra mèo bổ sung . Xem ví dụ pdftk
Giải pháp của myokyawhtun phù hợp nhất với tôi (sử dụng PHP 5.4)
Mặc dù vậy, bạn vẫn sẽ gặp lỗi - Tôi đã giải quyết bằng cách sử dụng cách sau:
Dòng 269 của fpdf_tpl.php - đã thay đổi các tham số hàm thành:
function Image($file, $x=null, $y=null, $w=0, $h=0, $type='', $link='',$align='', $resize=false, $dpi=300, $palign='', $ismask=false, $imgmask=false, $border=0) {
Tôi cũng đã thực hiện thay đổi tương tự trên dòng 898 của fpdf.php