Hướng dẫn pdftk php - pdftk php

php-pdftk

Hướng dẫn pdftk php - pdftk php

Chuyển đổi PDF và hình thức tiện ích dựa trên PDFTK.

Đặc trưng

PHP -PDFTK mang toàn bộ sức mạnh của

use mikehaertl\pdftk\Pdf;

// Fill form with data array
$pdf = new Pdf('/full/path/to/form.pdf');
$result = $pdf->fillForm([
        'name'=>'ÄÜÖ äüö мирано čárka',
        'nested.name' => 'valX',
    ])
    ->needAppearances()
    ->saveAs('filled.pdf');

// Always check for errors
if ($result === false) {
    $error = $pdf->getError();
}

// Fill form from FDF
$pdf = new Pdf('form.pdf');
$result = $pdf->fillForm('data.xfdf')
    ->saveAs('filled.pdf');
if ($result === false) {
    $error = $pdf->getError();
}
0 cho PHP - và hơn thế nữa.

  • Điền vào các biểu mẫu, từ tệp XFDF/FDF hoặc từ mảng dữ liệu (UTF-8 an toàn cho các biểu mẫu chưa được xử lý, yêu cầu PDFTK 2.x!)
  • Tạo các tệp XFDF hoặc FDF từ các mảng PHP (an toàn UTF-8!)
  • Tạo các tệp FDF từ các biểu mẫu PDF đã điền
  • Kết hợp các trang từ một số tệp PDF vào tệp PDF mới
  • Chia PDF thành một tệp trên mỗi trang
  • Thêm nền hoặc lớp PDF lớp phủ
  • Đọc dữ liệu meta về pdf và các trường biểu mẫu
  • Đặt mật khẩu và quyền
  • Xóa mật khẩu

Yêu cầu

  • Lệnh
    use mikehaertl\pdftk\Pdf;
    
    // Fill form with data array
    $pdf = new Pdf('/full/path/to/form.pdf');
    $result = $pdf->fillForm([
            'name'=>'ÄÜÖ äüö мирано čárka',
            'nested.name' => 'valX',
        ])
        ->needAppearances()
        ->saveAs('filled.pdf');
    
    // Always check for errors
    if ($result === false) {
        $error = $pdf->getError();
    }
    
    // Fill form from FDF
    $pdf = new Pdf('form.pdf');
    $result = $pdf->fillForm('data.xfdf')
        ->saveAs('filled.pdf');
    if ($result === false) {
        $error = $pdf->getError();
    }
    0 phải được cài đặt và hoạt động trên hệ thống của bạn
  • Thư viện này được viết cho các phiên bản PDFTK 2.x. Bạn sẽ có thể sử dụng nó với PDFTK 1.x nhưng không phải tất cả các phương thức sẽ hoạt động ở đó. Để biết chi tiết tham khảo trang người đàn ông của PDFTK trên hệ thống của bạn.
  • Có một vấn đề đã biết trên Ubuntu nếu bạn đã cài đặt gói
    use mikehaertl\pdftk\Pdf;
    
    // Fill form with data array
    $pdf = new Pdf('/full/path/to/form.pdf');
    $result = $pdf->fillForm([
            'name'=>'ÄÜÖ äüö мирано čárka',
            'nested.name' => 'valX',
        ])
        ->needAppearances()
        ->saveAs('filled.pdf');
    
    // Always check for errors
    if ($result === false) {
        $error = $pdf->getError();
    }
    
    // Fill form from FDF
    $pdf = new Pdf('form.pdf');
    $result = $pdf->fillForm('data.xfdf')
        ->saveAs('filled.pdf');
    if ($result === false) {
        $error = $pdf->getError();
    }
    0 từ Snap. Phiên bản này không có quyền ghi vào thư mục
    use mikehaertl\pdftk\Pdf;
    
    // Fill form with data array
    $pdf = new Pdf('/full/path/to/form.pdf');
    $result = $pdf->fillForm([
            'name'=>'ÄÜÖ äüö мирано čárka',
            'nested.name' => 'valX',
        ])
        ->needAppearances()
        ->saveAs('filled.pdf');
    
    // Always check for errors
    if ($result === false) {
        $error = $pdf->getError();
    }
    
    // Fill form from FDF
    $pdf = new Pdf('form.pdf');
    $result = $pdf->fillForm('data.xfdf')
        ->saveAs('filled.pdf');
    if ($result === false) {
        $error = $pdf->getError();
    }
    3. Bạn có thể đặt một thư mục Temporay khác như được mô tả bên dưới hoặc sử dụng gói khác. Đối với Ubuntu 18.10, cũng có gói
    use mikehaertl\pdftk\Pdf;
    
    // Fill form with data array
    $pdf = new Pdf('/full/path/to/form.pdf');
    $result = $pdf->fillForm([
            'name'=>'ÄÜÖ äüö мирано čárka',
            'nested.name' => 'valX',
        ])
        ->needAppearances()
        ->saveAs('filled.pdf');
    
    // Always check for errors
    if ($result === false) {
        $error = $pdf->getError();
    }
    
    // Fill form from FDF
    $pdf = new Pdf('form.pdf');
    $result = $pdf->fillForm('data.xfdf')
        ->saveAs('filled.pdf');
    if ($result === false) {
        $error = $pdf->getError();
    }
    4 có sẵn thông qua APT sẽ hoạt động tốt. Bạn cũng có thể cài đặt gói này trên Ubuntu 18.04 nếu bạn tải xuống thủ công. Cũng kiểm tra câu trả lời này trên Askubuntu.

Lưu ý: Phiên bản PDFTK từ PPA

use mikehaertl\pdftk\Pdf;

// Fill form with data array
$pdf = new Pdf('/full/path/to/form.pdf');
$result = $pdf->fillForm([
        'name'=>'ÄÜÖ äüö мирано čárka',
        'nested.name' => 'valX',
    ])
    ->needAppearances()
    ->saveAs('filled.pdf');

// Always check for errors
if ($result === false) {
    $error = $pdf->getError();
}

// Fill form from FDF
$pdf = new Pdf('form.pdf');
$result = $pdf->fillForm('data.xfdf')
    ->saveAs('filled.pdf');
if ($result === false) {
    $error = $pdf->getError();
}
5 thay thế không còn khả dụng. Thay vào đó, tác giả bây giờ chỉ ra câu trả lời của mình trên Askubuntu liên kết ở trên. The pdftk version from the alternative PPA
use mikehaertl\pdftk\Pdf;

// Fill form with data array
$pdf = new Pdf('/full/path/to/form.pdf');
$result = $pdf->fillForm([
        'name'=>'ÄÜÖ äüö мирано čárka',
        'nested.name' => 'valX',
    ])
    ->needAppearances()
    ->saveAs('filled.pdf');

// Always check for errors
if ($result === false) {
    $error = $pdf->getError();
}

// Fill form from FDF
$pdf = new Pdf('form.pdf');
$result = $pdf->fillForm('data.xfdf')
    ->saveAs('filled.pdf');
if ($result === false) {
    $error = $pdf->getError();
}
5 is no longer available. The author instead now points to his answer on askubuntu linked above.

Cài đặt

Bạn nên sử dụng nhà soạn nhạc để cài đặt thư viện này.

composer require mikehaertl/php-pdftk

Ví dụ

Tạo phiên bản cho các tệp PDF

Có một số cách để cho biết phiên bản

use mikehaertl\pdftk\Pdf;

// Fill form with data array
$pdf = new Pdf('/full/path/to/form.pdf');
$result = $pdf->fillForm([
        'name'=>'ÄÜÖ äüö мирано čárka',
        'nested.name' => 'valX',
    ])
    ->needAppearances()
    ->saveAs('filled.pdf');

// Always check for errors
if ($result === false) {
    $error = $pdf->getError();
}

// Fill form from FDF
$pdf = new Pdf('form.pdf');
$result = $pdf->fillForm('data.xfdf')
    ->saveAs('filled.pdf');
if ($result === false) {
    $error = $pdf->getError();
}
6 mà tệp mà nó nên sử dụng. Một số tệp cũng có thể yêu cầu mật khẩu hoặc cần một bí danh được sử dụng làm xử lý trong một số hoạt động (ví dụ: Cat hoặc Shuffle).

Lưu ý: Trong phiên bản 2.x của PDFTK, tay cầm có thể là một hoặc nhiều chữ cái trên. In version 2.x of pdftk a handle can be one or more upper case letters.

// Create an instance for a single file
$pdf = new Pdf('/path/to/form.pdf');

// Alternatively add files later. Handles are autogenerated in this case.
$pdf = new Pdf();
$pdf->addFile('/path/to/file1.pdf');
$pdf->addFile('/path/to/file2.pdf');

// Add files with own handle
$pdf = new Pdf();
$pdf->addFile('/path/to/file1.pdf', 'A');
$pdf->addFile('/path/to/file2.pdf', 'B');
// Add file with handle and password
$pdf->addFile('/path/to/file3.pdf', 'C', 'secret*password');

// Shortcut to pass all files to the constructor
$pdf = new Pdf([
  'A' => ['/path/to/file1.pdf', 'secret*password1'],
  'B' => ['/path/to/file2.pdf', 'secret*password2'],
]);

Hoạt động

Vui lòng tham khảo trang người đàn ông

use mikehaertl\pdftk\Pdf;

// Fill form with data array
$pdf = new Pdf('/full/path/to/form.pdf');
$result = $pdf->fillForm([
        'name'=>'ÄÜÖ äüö мирано čárka',
        'nested.name' => 'valX',
    ])
    ->needAppearances()
    ->saveAs('filled.pdf');

// Always check for errors
if ($result === false) {
    $error = $pdf->getError();
}

// Fill form from FDF
$pdf = new Pdf('form.pdf');
$result = $pdf->fillForm('data.xfdf')
    ->saveAs('filled.pdf');
if ($result === false) {
    $error = $pdf->getError();
}
0 cho mỗi thao tác để tìm hiểu cách mỗi hoạt động hoạt động chi tiết và có sẵn tùy chọn nào.

Đối với tất cả các hoạt động, bạn có thể lưu pdf cục bộ thông qua

use mikehaertl\pdftk\Pdf;

// Fill form with data array
$pdf = new Pdf('/full/path/to/form.pdf');
$result = $pdf->fillForm([
        'name'=>'ÄÜÖ äüö мирано čárka',
        'nested.name' => 'valX',
    ])
    ->needAppearances()
    ->saveAs('filled.pdf');

// Always check for errors
if ($result === false) {
    $error = $pdf->getError();
}

// Fill form from FDF
$pdf = new Pdf('form.pdf');
$result = $pdf->fillForm('data.xfdf')
    ->saveAs('filled.pdf');
if ($result === false) {
    $error = $pdf->getError();
}
8 hoặc gửi nó đến trình duyệt với
use mikehaertl\pdftk\Pdf;

// Fill form with data array
$pdf = new Pdf('/full/path/to/form.pdf');
$result = $pdf->fillForm([
        'name'=>'ÄÜÖ äüö мирано čárka',
        'nested.name' => 'valX',
    ])
    ->needAppearances()
    ->saveAs('filled.pdf');

// Always check for errors
if ($result === false) {
    $error = $pdf->getError();
}

// Fill form from FDF
$pdf = new Pdf('form.pdf');
$result = $pdf->fillForm('data.xfdf')
    ->saveAs('filled.pdf');
if ($result === false) {
    $error = $pdf->getError();
}
9. Nếu bạn chuyển tên tệp cho
use mikehaertl\pdftk\Pdf;

// Fill form with data array
$pdf = new Pdf('/full/path/to/form.pdf');
$result = $pdf->fillForm($data)
    ->replacementFont('/usr/share/fonts/dejavu/DejaVuSans.ttf')
    ->saveAs('filled.pdf');
0 Trình duyệt máy khách sẽ mở đoạn hội thoại tải xuống trong khi không có tên tệp, nó thường sẽ hiển thị nội tuyến PDF.

Quan trọng: Bạn luôn chỉ có thể thực hiện một trong các hoạt động sau trên một phiên bản PDF duy nhất. Dưới đây bạn có thể tìm thấy một cách giải quyết nếu bạn cần nhiều hoạt động.

Điền vào mẫu

Điền vào biểu mẫu PDF với dữ liệu từ mảng PHP hoặc tệp XFDF/FDF.

use mikehaertl\pdftk\Pdf;

// Fill form with data array
$pdf = new Pdf('/full/path/to/form.pdf');
$result = $pdf->fillForm([
        'name'=>'ÄÜÖ äüö мирано čárka',
        'nested.name' => 'valX',
    ])
    ->needAppearances()
    ->saveAs('filled.pdf');

// Always check for errors
if ($result === false) {
    $error = $pdf->getError();
}

// Fill form from FDF
$pdf = new Pdf('form.pdf');
$result = $pdf->fillForm('data.xfdf')
    ->saveAs('filled.pdf');
if ($result === false) {
    $error = $pdf->getError();
}

Lưu ý: Khi điền vào dữ liệu UTF-8, bạn phải luôn thêm tùy chọn

use mikehaertl\pdftk\Pdf;

// Fill form with data array
$pdf = new Pdf('/full/path/to/form.pdf');
$result = $pdf->fillForm($data)
    ->replacementFont('/usr/share/fonts/dejavu/DejaVuSans.ttf')
    ->saveAs('filled.pdf');
1. Điều này sẽ đảm bảo rằng người đọc PDF chăm sóc việc sử dụng các phông chữ phù hợp để kết xuất, điều mà PDFTK không thể làm cho bạn. Cũng lưu ý rằng
use mikehaertl\pdftk\Pdf;

// Fill form with data array
$pdf = new Pdf('/full/path/to/form.pdf');
$result = $pdf->fillForm($data)
    ->replacementFont('/usr/share/fonts/dejavu/DejaVuSans.ttf')
    ->saveAs('filled.pdf');
2 không thực sự hoạt động tốt nếu bạn có ký tự đặc biệt trong dữ liệu của mình.
When filling in UTF-8 data, you should always add the
use mikehaertl\pdftk\Pdf;

// Fill form with data array
$pdf = new Pdf('/full/path/to/form.pdf');
$result = $pdf->fillForm($data)
    ->replacementFont('/usr/share/fonts/dejavu/DejaVuSans.ttf')
    ->saveAs('filled.pdf');
1 option. This will make sure, that the PDF reader takes care of using the right fonts for rendering, something that pdftk can't do for you. Also note that
use mikehaertl\pdftk\Pdf;

// Fill form with data array
$pdf = new Pdf('/full/path/to/form.pdf');
$result = $pdf->fillForm($data)
    ->replacementFont('/usr/share/fonts/dejavu/DejaVuSans.ttf')
    ->saveAs('filled.pdf');
2 doesn't really work well if you have special characters in your data.

Nếu bạn sử dụng

use mikehaertl\pdftk\Pdf;

// Fill form with data array
$pdf = new Pdf('/full/path/to/form.pdf');
$result = $pdf->fillForm([
        'name'=>'ÄÜÖ äüö мирано čárka',
        'nested.name' => 'valX',
    ])
    ->needAppearances()
    ->saveAs('filled.pdf');

// Always check for errors
if ($result === false) {
    $error = $pdf->getError();
}

// Fill form from FDF
$pdf = new Pdf('form.pdf');
$result = $pdf->fillForm('data.xfdf')
    ->saveAs('filled.pdf');
if ($result === false) {
    $error = $pdf->getError();
}
4> = 3.3.0 và phông chữ được nhúng không hỗ trợ các ký tự UTF-8, bạn cũng có thể thay thế nó bằng phông chữ cục bộ:

use mikehaertl\pdftk\Pdf;

// Fill form with data array
$pdf = new Pdf('/full/path/to/form.pdf');
$result = $pdf->fillForm($data)
    ->replacementFont('/usr/share/fonts/dejavu/DejaVuSans.ttf')
    ->saveAs('filled.pdf');

Tạo tệp XFDF/FDF từ mảng PHP

Đây là một tính năng tiền thưởng không có sẵn từ

use mikehaertl\pdftk\Pdf;

// Fill form with data array
$pdf = new Pdf('/full/path/to/form.pdf');
$result = $pdf->fillForm([
        'name'=>'ÄÜÖ äüö мирано čárka',
        'nested.name' => 'valX',
    ])
    ->needAppearances()
    ->saveAs('filled.pdf');

// Always check for errors
if ($result === false) {
    $error = $pdf->getError();
}

// Fill form from FDF
$pdf = new Pdf('form.pdf');
$result = $pdf->fillForm('data.xfdf')
    ->saveAs('filled.pdf');
if ($result === false) {
    $error = $pdf->getError();
}
0.

use mikehaertl\pdftk\XfdfFile;
use mikehaertl\pdftk\FdfFile;

$xfdf = new XfdfFile(['name' => 'Jürgen мирано']);
$xfdf->saveAs('/path/to/data.xfdf');

$fdf = new FdfFile(['name' => 'Jürgen мирано']);
$fdf->saveAs('/path/to/data.fdf');

Con mèo

Lắp một tệp PDF từ các trang từ một hoặc nhiều tệp PDF.

use mikehaertl\pdftk\Pdf;

// Extract pages 1-5 and 7,4,9 into a new file
$pdf = new Pdf('/path/to/my.pdf');
$result = $pdf->cat(1, 5)
    ->cat([7, 4, 9])
    ->saveAs('/path/to/new.pdf');
if ($result === false) {
    $error = $pdf->getError();
}

// Combine pages from several files
$pdf = new Pdf([
    'A' => '/path/file1.pdf',                 // A is alias for file1.pdf
    'B' => ['/path/file2.pdf','pass**word'],  // B is alias for file2.pdf
    'C' => ['/path/file3.pdf','secret**pw'],  // C is alias for file3.pdf
]);
$result = $pdf->cat(1, 5, 'A')                // pages 1-5 from A
    ->cat(3, null, 'B')             // page 3 from B
    ->cat(7, 'end', 'B', null, 'east') // pages 7-end from B, rotated East
    ->cat('end',3,'A','even')       // even pages 3-end in reverse order from A
    ->cat([2,3,7], 'C')             // pages 2,3 and 7 from C
    ->saveAs('/path/new.pdf');
if ($result === false) {
    $error = $pdf->getError();
}

Xáo trộn

Giống như

use mikehaertl\pdftk\Pdf;

// Fill form with data array
$pdf = new Pdf('/full/path/to/form.pdf');
$result = $pdf->fillForm($data)
    ->replacementFont('/usr/share/fonts/dejavu/DejaVuSans.ttf')
    ->saveAs('filled.pdf');
5 nhưng tạo "luồng" và điền PDF mới bằng một trang từ mỗi luồng tại một thời điểm.

use mikehaertl\pdftk\Pdf;

$pdf = new Pdf([
    'A' => '/path/file1.pdf',     // A is alias for file1.pdf
    'B' => '/path/file2.pdf',     // B is alias for file2.pdf
]);

// new.pdf will have pages A1, B3, A2, B4, A3, B5, ...
$result = $pdf->shuffle(1, 5, 'A')    // pages 1-5 from A
    ->shuffle(3, 8, 'B')    // pages 3-8 from B
    ->saveAs('/path/new.pdf');
if ($result === false) {
    $error = $pdf->getError();
}

Nổ

Chia tệp PDF thành một tệp trên mỗi trang.

use mikehaertl\pdftk\Pdf;

$pdf = new Pdf('/path/my.pdf');
$result = $pdf->burst('/path/page_%d.pdf');     // Supply a printf() pattern
if ($result === false) {
    $error = $pdf->getError();
}

Thêm nền PDF

Thêm một tệp PDF khác làm nền.

use mikehaertl\pdftk\Pdf;

// Set background from another PDF (first page repeated)
$pdf = new Pdf('/path/my.pdf');
$result = $pdf->background('/path/back.pdf')
    ->saveAs('/path/watermarked.pdf');
if ($result === false) {
    $error = $pdf->getError();
}

// Set background from another PDF (one page each)
$pdf = new Pdf('/path/my.pdf');
$result = $pdf->multiBackground('/path/back_pages.pdf')
    ->saveAs('/path/watermarked.pdf');
if ($result === false) {
    $error = $pdf->getError();
}

Thêm PDF lớp phủ

Thêm một tệp PDF khác dưới dạng lớp phủ.

use mikehaertl\pdftk\Pdf;

// Stamp with another PDF (first page repeated)
$pdf = new Pdf('/path/my.pdf');
$result = $pdf->stamp('/path/overlay.pdf')
    ->saveAs('/path/stamped.pdf');
if ($result === false) {
    $error = $pdf->getError();
}

// Stamp with another PDF (one page each)
$pdf = new Pdf('/path/my.pdf');
$result = $pdf->multiStamp('/path/overlay_pages.pdf')
    ->saveAs('/path/stamped.pdf');
if ($result === false) {
    $error = $pdf->getError();
}

Đính kèm tệp

Thêm tệp đính kèm tệp vào tài liệu hoặc vào một trang cụ thể.

// Create an instance for a single file
$pdf = new Pdf('/path/to/form.pdf');

// Alternatively add files later. Handles are autogenerated in this case.
$pdf = new Pdf();
$pdf->addFile('/path/to/file1.pdf');
$pdf->addFile('/path/to/file2.pdf');

// Add files with own handle
$pdf = new Pdf();
$pdf->addFile('/path/to/file1.pdf', 'A');
$pdf->addFile('/path/to/file2.pdf', 'B');
// Add file with handle and password
$pdf->addFile('/path/to/file3.pdf', 'C', 'secret*password');

// Shortcut to pass all files to the constructor
$pdf = new Pdf([
  'A' => ['/path/to/file1.pdf', 'secret*password1'],
  'B' => ['/path/to/file2.pdf', 'secret*password2'],
]);
0

Giải nén các tập tin

Sao chép tệp đính kèm từ tệp PDF đến thư mục đã cho.

// Create an instance for a single file
$pdf = new Pdf('/path/to/form.pdf');

// Alternatively add files later. Handles are autogenerated in this case.
$pdf = new Pdf();
$pdf->addFile('/path/to/file1.pdf');
$pdf->addFile('/path/to/file2.pdf');

// Add files with own handle
$pdf = new Pdf();
$pdf->addFile('/path/to/file1.pdf', 'A');
$pdf->addFile('/path/to/file2.pdf', 'B');
// Add file with handle and password
$pdf->addFile('/path/to/file3.pdf', 'C', 'secret*password');

// Shortcut to pass all files to the constructor
$pdf = new Pdf([
  'A' => ['/path/to/file1.pdf', 'secret*password1'],
  'B' => ['/path/to/file2.pdf', 'secret*password2'],
]);
1

Tạo FDF

Tạo một tệp FDF từ một biểu mẫu PDF được điền nhất định.

// Create an instance for a single file
$pdf = new Pdf('/path/to/form.pdf');

// Alternatively add files later. Handles are autogenerated in this case.
$pdf = new Pdf();
$pdf->addFile('/path/to/file1.pdf');
$pdf->addFile('/path/to/file2.pdf');

// Add files with own handle
$pdf = new Pdf();
$pdf->addFile('/path/to/file1.pdf', 'A');
$pdf->addFile('/path/to/file2.pdf', 'B');
// Add file with handle and password
$pdf->addFile('/path/to/file3.pdf', 'C', 'secret*password');

// Shortcut to pass all files to the constructor
$pdf = new Pdf([
  'A' => ['/path/to/file1.pdf', 'secret*password1'],
  'B' => ['/path/to/file2.pdf', 'secret*password2'],
]);
2

Nhận dữ liệu PDF

Đọc ra siêu dữ liệu hoặc hình thức thông tin trường từ tệp PDF.

// Create an instance for a single file
$pdf = new Pdf('/path/to/form.pdf');

// Alternatively add files later. Handles are autogenerated in this case.
$pdf = new Pdf();
$pdf->addFile('/path/to/file1.pdf');
$pdf->addFile('/path/to/file2.pdf');

// Add files with own handle
$pdf = new Pdf();
$pdf->addFile('/path/to/file1.pdf', 'A');
$pdf->addFile('/path/to/file2.pdf', 'B');
// Add file with handle and password
$pdf->addFile('/path/to/file3.pdf', 'C', 'secret*password');

// Shortcut to pass all files to the constructor
$pdf = new Pdf([
  'A' => ['/path/to/file1.pdf', 'secret*password1'],
  'B' => ['/path/to/file2.pdf', 'secret*password2'],
]);
3

Cách thực hiện nhiều hơn một thao tác trên PDF

Như đã nêu ở trên, bạn chỉ có thể thực hiện một trong các hoạt động trước trên một phiên bản PDF duy nhất. Nếu bạn cần nhiều hơn một thao tác, bạn có thể đưa một phiên bản

use mikehaertl\pdftk\Pdf;

// Fill form with data array
$pdf = new Pdf('/full/path/to/form.pdf');
$result = $pdf->fillForm([
        'name'=>'ÄÜÖ äüö мирано čárka',
        'nested.name' => 'valX',
    ])
    ->needAppearances()
    ->saveAs('filled.pdf');

// Always check for errors
if ($result === false) {
    $error = $pdf->getError();
}

// Fill form from FDF
$pdf = new Pdf('form.pdf');
$result = $pdf->fillForm('data.xfdf')
    ->saveAs('filled.pdf');
if ($result === false) {
    $error = $pdf->getError();
}
6 vào một hoạt động khác:

// Create an instance for a single file
$pdf = new Pdf('/path/to/form.pdf');

// Alternatively add files later. Handles are autogenerated in this case.
$pdf = new Pdf();
$pdf->addFile('/path/to/file1.pdf');
$pdf->addFile('/path/to/file2.pdf');

// Add files with own handle
$pdf = new Pdf();
$pdf->addFile('/path/to/file1.pdf', 'A');
$pdf->addFile('/path/to/file2.pdf', 'B');
// Add file with handle and password
$pdf->addFile('/path/to/file3.pdf', 'C', 'secret*password');

// Shortcut to pass all files to the constructor
$pdf = new Pdf([
  'A' => ['/path/to/file1.pdf', 'secret*password1'],
  'B' => ['/path/to/file2.pdf', 'secret*password2'],
]);
4

Tùy chọn

Bạn có thể kết hợp các hoạt động trên với một hoặc nhiều tùy chọn sau.

// Create an instance for a single file
$pdf = new Pdf('/path/to/form.pdf');

// Alternatively add files later. Handles are autogenerated in this case.
$pdf = new Pdf();
$pdf->addFile('/path/to/file1.pdf');
$pdf->addFile('/path/to/file2.pdf');

// Add files with own handle
$pdf = new Pdf();
$pdf->addFile('/path/to/file1.pdf', 'A');
$pdf->addFile('/path/to/file2.pdf', 'B');
// Add file with handle and password
$pdf->addFile('/path/to/file3.pdf', 'C', 'secret*password');

// Shortcut to pass all files to the constructor
$pdf = new Pdf([
  'A' => ['/path/to/file1.pdf', 'secret*password1'],
  'B' => ['/path/to/file2.pdf', 'secret*password2'],
]);
5

Lệnh shell

Lớp sử dụng PHP-ShellCommand để thực thi

use mikehaertl\pdftk\Pdf;

// Fill form with data array
$pdf = new Pdf('/full/path/to/form.pdf');
$result = $pdf->fillForm([
        'name'=>'ÄÜÖ äüö мирано čárka',
        'nested.name' => 'valX',
    ])
    ->needAppearances()
    ->saveAs('filled.pdf');

// Always check for errors
if ($result === false) {
    $error = $pdf->getError();
}

// Fill form from FDF
$pdf = new Pdf('form.pdf');
$result = $pdf->fillForm('data.xfdf')
    ->saveAs('filled.pdf');
if ($result === false) {
    $error = $pdf->getError();
}
0. Bạn có thể vượt qua
use mikehaertl\pdftk\Pdf;

// Fill form with data array
$pdf = new Pdf('/full/path/to/form.pdf');
$result = $pdf->fillForm($data)
    ->replacementFont('/usr/share/fonts/dejavu/DejaVuSans.ttf')
    ->saveAs('filled.pdf');
8 cho lớp
use mikehaertl\pdftk\Pdf;

// Fill form with data array
$pdf = new Pdf('/full/path/to/form.pdf');
$result = $pdf->fillForm($data)
    ->replacementFont('/usr/share/fonts/dejavu/DejaVuSans.ttf')
    ->saveAs('filled.pdf');
9 của nó dưới dạng đối số thứ hai cho hàm tạo:

// Create an instance for a single file
$pdf = new Pdf('/path/to/form.pdf');

// Alternatively add files later. Handles are autogenerated in this case.
$pdf = new Pdf();
$pdf->addFile('/path/to/file1.pdf');
$pdf->addFile('/path/to/file2.pdf');

// Add files with own handle
$pdf = new Pdf();
$pdf->addFile('/path/to/file1.pdf', 'A');
$pdf->addFile('/path/to/file2.pdf', 'B');
// Add file with handle and password
$pdf->addFile('/path/to/file3.pdf', 'C', 'secret*password');

// Shortcut to pass all files to the constructor
$pdf = new Pdf([
  'A' => ['/path/to/file1.pdf', 'secret*password1'],
  'B' => ['/path/to/file2.pdf', 'secret*password2'],
]);
6

Giải quyết các vấn đề với các ký tự UTF-8 trong tên tệp hoặc nội dung vô hạn

Nếu bạn có các tệp có các ký tự được mã hóa UTF-8 trong tên tệp của họ hoặc nếu bạn chuyển một infofile với các ký tự như vậy cho

use mikehaertl\pdftk\XfdfFile;
use mikehaertl\pdftk\FdfFile;

$xfdf = new XfdfFile(['name' => 'Jürgen мирано']);
$xfdf->saveAs('/path/to/data.xfdf');

$fdf = new FdfFile(['name' => 'Jürgen мирано']);
$fdf->saveAs('/path/to/data.fdf');
0, bạn nên cung cấp đúng địa điểm khi cắt bỏ
use mikehaertl\pdftk\Pdf;

// Fill form with data array
$pdf = new Pdf('/full/path/to/form.pdf');
$result = $pdf->fillForm([
        'name'=>'ÄÜÖ äüö мирано čárka',
        'nested.name' => 'valX',
    ])
    ->needAppearances()
    ->saveAs('filled.pdf');

// Always check for errors
if ($result === false) {
    $error = $pdf->getError();
}

// Fill form from FDF
$pdf = new Pdf('form.pdf');
$result = $pdf->fillForm('data.xfdf')
    ->saveAs('filled.pdf');
if ($result === false) {
    $error = $pdf->getError();
}
0. Do đó, bạn có thể thêm các tùy chọn sau:

// Create an instance for a single file
$pdf = new Pdf('/path/to/form.pdf');

// Alternatively add files later. Handles are autogenerated in this case.
$pdf = new Pdf();
$pdf->addFile('/path/to/file1.pdf');
$pdf->addFile('/path/to/file2.pdf');

// Add files with own handle
$pdf = new Pdf();
$pdf->addFile('/path/to/file1.pdf', 'A');
$pdf->addFile('/path/to/file2.pdf', 'B');
// Add file with handle and password
$pdf->addFile('/path/to/file3.pdf', 'C', 'secret*password');

// Shortcut to pass all files to the constructor
$pdf = new Pdf([
  'A' => ['/path/to/file1.pdf', 'secret*password1'],
  'B' => ['/path/to/file2.pdf', 'secret*password2'],
]);
7

Lưu ý: Bạn cần đảm bảo rằng địa phương bạn đặt ở đây có sẵn trên hệ thống của bạn. Trên Linux, bạn có thể kiểm tra với

use mikehaertl\pdftk\XfdfFile;
use mikehaertl\pdftk\FdfFile;

$xfdf = new XfdfFile(['name' => 'Jürgen мирано']);
$xfdf->saveAs('/path/to/data.xfdf');

$fdf = new FdfFile(['name' => 'Jürgen мирано']);
$fdf->saveAs('/path/to/data.fdf');
2 địa phương nào được cài đặt. Bài viết này giải thích khái niệm chi tiết hơn. You need to ensure that the locale you set here is available on your system. On Linux you can check with
use mikehaertl\pdftk\XfdfFile;
use mikehaertl\pdftk\FdfFile;

$xfdf = new XfdfFile(['name' => 'Jürgen мирано']);
$xfdf->saveAs('/path/to/data.xfdf');

$fdf = new FdfFile(['name' => 'Jürgen мирано']);
$fdf->saveAs('/path/to/data.fdf');
2 which locales are installed. This article explains the concept in more detail.

Tập tin tạm thời

Trong nội bộ, một tệp tạm thời được tạo thông qua PHP-TMPFILE.Bạn cũng có thể truy cập trực tiếp tệp đó, ví dụ:Nếu bạn không muốn gửi hoặc lưu tệp nhưng chỉ cần nội dung pdf nhị phân:

// Create an instance for a single file
$pdf = new Pdf('/path/to/form.pdf');

// Alternatively add files later. Handles are autogenerated in this case.
$pdf = new Pdf();
$pdf->addFile('/path/to/file1.pdf');
$pdf->addFile('/path/to/file2.pdf');

// Add files with own handle
$pdf = new Pdf();
$pdf->addFile('/path/to/file1.pdf', 'A');
$pdf->addFile('/path/to/file2.pdf', 'B');
// Add file with handle and password
$pdf->addFile('/path/to/file3.pdf', 'C', 'secret*password');

// Shortcut to pass all files to the constructor
$pdf = new Pdf([
  'A' => ['/path/to/file1.pdf', 'secret*password1'],
  'B' => ['/path/to/file2.pdf', 'secret*password2'],
]);
8

Nếu bạn có vấn đề về quyền, bạn có thể phải đặt một thư mục trong đó lệnh

use mikehaertl\pdftk\Pdf;

// Fill form with data array
$pdf = new Pdf('/full/path/to/form.pdf');
$result = $pdf->fillForm([
        'name'=>'ÄÜÖ äüö мирано čárka',
        'nested.name' => 'valX',
    ])
    ->needAppearances()
    ->saveAs('filled.pdf');

// Always check for errors
if ($result === false) {
    $error = $pdf->getError();
}

// Fill form from FDF
$pdf = new Pdf('form.pdf');
$result = $pdf->fillForm('data.xfdf')
    ->saveAs('filled.pdf');
if ($result === false) {
    $error = $pdf->getError();
}
0 của bạn có thể viết cho:

// Create an instance for a single file
$pdf = new Pdf('/path/to/form.pdf');

// Alternatively add files later. Handles are autogenerated in this case.
$pdf = new Pdf();
$pdf->addFile('/path/to/file1.pdf');
$pdf->addFile('/path/to/file2.pdf');

// Add files with own handle
$pdf = new Pdf();
$pdf->addFile('/path/to/file1.pdf', 'A');
$pdf->addFile('/path/to/file2.pdf', 'B');
// Add file with handle and password
$pdf->addFile('/path/to/file3.pdf', 'C', 'secret*password');

// Shortcut to pass all files to the constructor
$pdf = new Pdf([
  'A' => ['/path/to/file1.pdf', 'secret*password1'],
  'B' => ['/path/to/file2.pdf', 'secret*password2'],
]);
9

API

Vui lòng tham khảo các tệp nguồn để biết tài liệu đầy đủ của từng phương thức.