|
<?php |
|
/** |
|
* DOMPDF/FPDI Example |
|
* 1. create pdf with custom fonts in dompdf |
|
* 2. encrypt pdf with fpdi |
|
*/ |
|
|
|
use Dompdf\Adapter\CPDF; |
|
use Dompdf\Dompdf; |
|
use Dompdf\Exception; |
|
|
|
function pdfEncrypt ($origFile, $destFile, $owner_password=null, $user_password=null){ |
|
require_once('vendor/fpdf181/fpdf.php'); |
|
require_once('vendor/FPDI-1.6.1/fpdi.php'); |
|
require_once('vendor/FPDI-1.6.1/FPDI_Protection.php'); |
|
$pdf = new FPDI_Protection(); |
|
$pagecount = $pdf->setSourceFile($origFile); |
|
|
|
// copy all pages from the old unprotected pdf in the new one |
|
for ($loop = 1; $loop <= $pagecount; $loop++) { |
|
$tplidx = $pdf->importPage($loop); |
|
$pdf->addPage(); |
|
$dim = $pdf->useTemplate($tplidx); |
|
//var_dump($dim);exit; |
|
} |
|
// Allow for array('print', 'modify', 'copy', 'annot-forms'); |
|
$pdf->SetProtection(array('print'),$user_password, $owner_password); |
|
$pdf->Output($destFile,'F'); // F write, D download |
|
return $destFile; |
|
} |
|
|
|
require "vendor/dompdf/autoload.inc.php"; |
|
|
|
$file_to_save = 'content.pdf'; |
|
|
|
//password for the pdf file |
|
$owner_password = "owner"; |
|
$user_password = null; // everybody can open the file! |
|
|
|
//name of the destination file |
|
$destFile =str_replace('.pdf','_protected.pdf',$file_to_save); |
|
|
|
// html content from file |
|
$html = file_get_contents('content.html'); |
|
|
|
// start with DOMPDF |
|
$dompdf = new DOMPDF(); //if you use namespaces you may use new \DOMPDF() |
|
|
|
// set options: https://github.com/dompdf/dompdf/blob/master/src%2FOptions.php |
|
$dompdf->set_option('isRemoteEnabled',true); |
|
$dompdf->set_option('isFontSubsettingEnabled',true); |
|
$dompdf->set_option('tempDir','content/tmp'); |
|
|
|
$dompdf->set_paper('a4', 'portrait'); |
|
|
|
//generate |
|
$dompdf->loadHtml($html); |
|
$dompdf->render(); |
|
|
|
// embedded custom fonts won't be visible with encryption on |
|
// $dompdf->get_canvas()->get_cpdf()->setEncryption('', 'ownerpass', array('print','copy')); |
|
// $dompdf->stream("sample.pdf", array("Attachment"=>0)); |
|
|
|
//save the pdf file on the server |
|
file_put_contents($file_to_save, $dompdf->output()); |
|
|
|
//encrypt the pdf and create the protected file |
|
$file_to_save = pdfEncrypt($file_to_save, $destFile, $owner_password, $user_password ); |
|
|
|
//print the pdf file to the screen for saving |
|
header('Content-type: application/pdf'); |
|
header('Content-Disposition: inline; filename="file.pdf"'); |
|
header('Content-Transfer-Encoding: binary'); |
|
header('Content-Length: ' . filesize($file_to_save)); |
|
header('Accept-Ranges: bytes'); |
|
readfile($file_to_save); |