제 컨셉은 웹 사이트에 10개의 PDF 파일이 있다는 것입니다.사용자는 일부 PDF 파일을 선택한 다음 병합을 선택하여 선택한 페이지가 포함된 단일 PDF 파일을 만들 수 있습니다.어떻게 하면 php로 할 수 있을까요?
질문에 대한 답변
다음은 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);
내가 찾은 곳의 링크를 잊어버렸는데, 정상적으로 작동한다.
주의: 이 기능을 사용하려면 gs(Linux 및 Mac에 있음) 또는 Ghostscript(Windows에 있음)가 설치되어 있어야 합니다.
github.com의 PDF Merger를 제안합니다.이렇게 간단합니다.
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
전에 이걸 한 적이 있다.저는 fpdf로 생성한 PDF를 가지고 있었고, 거기에 다양한 양의 PDF를 추가해야 했습니다.
그래서 이미 fpdf 오브젝트와 페이지가 셋업되어 있습니다(http://www.fpdf.org/).fpdi를 사용하여 파일을 Import했습니다(http://www.setasign.de/products/pdf-php-solutions/fpdi/) FDPI는 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);
}
이것에 의해, 각 PDF 는 다른 PDF 에 넣을 수 있는 것입니다.그것은 내가 필요로 하는 것에 대해 놀라울 정도로 잘 작동했다.
$cmd = "gs -q -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -sOutputFile=".$new." ".implode(" ", $files); shell_exec($cmd);
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');