Advertisement
Guest User

Untitled

a guest
Apr 1st, 2020
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.90 KB | None | 0 0
  1. <?php
  2. class pdftkMerger {
  3.  
  4. private $localPdftk;
  5. private $tmpDir;
  6.  
  7. public function __construct($pdftk = '/usr/bin/pdftk', $tmp = '/tvitemp/tmp') {
  8. $this->localPdftk = $pdftk;
  9. $this->tmpDir = $tmp;
  10. }
  11.  
  12. private function getFileData($file) {
  13. $downloadFile = fopen($file, 'r');
  14. if ($downloadFile) {
  15. $buf = "";
  16. while(!feof($downloadFile)) {
  17. $buf .= fread($downloadFile, 1024 * 8);
  18. }
  19. fclose($downloadFile);
  20. }
  21. return $buf;
  22. }
  23.  
  24. private function getReportInfo($report) {
  25. $command = $this->localPdftk.' '.$report.' dump_data output '.$this->tmpDir.'/report.txt';
  26. exec($command);
  27. $info = $this->getFileData($this->tmpDir.'/report.txt');
  28. unlink($this->tmpDir.'/report.txt');
  29. return $info;
  30. }
  31.  
  32. private function getPageAmount($pdfInfo) {
  33. preg_match('/NumberOfPages: ([0-9]{1,})/', $pdfInfo, $matches);
  34. return $matches[1];
  35. }
  36.  
  37. public function getMergedPdf($original, $attachments) {
  38. if(count($attachments) > 0) {
  39. $originalInfo = $this->getReportInfo($original);
  40. $pageAmount = $this->getPageAmount($originalInfo);
  41. $pageAmount++;
  42. $mergedInfo = $originalInfo."\nBookmarkBegin\nBookmarkTitle: Verplichte bijlagen\nBookmarkLevel: 1\nBookmarkPageNumber: ".$pageAmount;
  43.  
  44. $mergeCommand = $this->localPdftk.' '.$original;
  45. foreach($attachments AS $name => $path) {
  46. $mime_type = mime_content_type($path);
  47. switch ($mime_type) {
  48. case 'application/pdf':
  49. case 'application/octet-stream':
  50. $mergeCommand .= ' '.$path;
  51. $mergedInfo .= "\nBookmarkBegin\nBookmarkTitle: ".$name."\nBookmarkLevel: 2\nBookmarkPageNumber: ".$pageAmount;
  52. $pageAmount = $pageAmount + $this->getPageAmount($this->getReportInfo($path));
  53. break;
  54. default:
  55. continue;
  56. }
  57. }
  58. $mergeCommand .= ' cat output '.$this->tmpDir.'/_taxatierapportMerged.pdf';
  59. exec($mergeCommand);
  60.  
  61. $fh = fopen($this->tmpDir.'/mergedInfo.txt', 'w');
  62. fwrite($fh,$mergedInfo);
  63. fclose($fh);
  64.  
  65. $updateInfoCommand = $this->localPdftk.' '.$this->tmpDir.'/_taxatierapportMerged.pdf update_info '.$this->tmpDir.'/mergedInfo.txt output '.$this->tmpDir.'/complete.pdf';
  66. exec($updateInfoCommand);
  67. $pdf = $this->getFileData($this->tmpDir.'/complete.pdf');
  68.  
  69. unlink($this->tmpDir.'/_taxatierapportMerged.pdf');
  70. unlink($this->tmpDir.'/mergedInfo.txt');
  71. unlink($this->tmpDir.'/complete.pdf');
  72.  
  73. return $pdf;
  74. }
  75. }
  76. }
  77.  
  78. $merger = new pdftkMerger();
  79.  
  80. $tmpDir = "/tvitemp/tmp";
  81. $original = "_test.pdf";
  82.  
  83. $attachments = array(
  84. 'opdrachtvoorwaarden' => $tmpDir.'/test.pdf',
  85. 'CRT' => $tmpDir.'/test.pdf',
  86. );
  87.  
  88. header("Content-type:application/pdf");
  89. echo $merger->getMergedPdf($tmpDir.'/'.$original, $attachments);
  90. die();
  91. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement