Search code examples
phpwordpresspdfpdf-generationdompdf

DomPDF - addInfo (base64_encode) and setEncryption


I am having an issue with DomPDF and base64_encode with setEncryption function.

This is a sample of my code.

    // Render the HTML as PDF
    $dompdf->render();

    //Encode title into base64 and add it to PDF Meta
    $title_64 = base64_encode( $title );
    $dompdf->getCanvas()->get_cpdf()->addInfo( 'Subject' , $title_64 );

    //Locking pdf to allow printing only
    $dompdf->getCanvas()->get_cpdf()->setEncryption( '' , '' , array( 'print' ) );

    // Output the generated PDF to Browser
    $dompdf->stream( $post->post_name . ".pdf" , array( "Attachment" => TRUE ) );

So pretty much what is happening once PDF is downloaded all meta data is missing.

As soon I remove base64_encode function that is wrapping $title Meta data is back.

Also if I keep base64_encode but I remove

$dompdf->getCanvas()->get_cpdf()->setEncryption( '' , '' , array( 'print' ) );

Everything seems to be working but I am able to modify PDF which I don't want.

As my end result I should be able only to print PDF which is what i have now + all Meta data to be

base64_encoded

Did anyone encounter similar issue ?


Solution

  • This is a bug in the CPDF class/version, which is used by DomPDF. The encrypted strings are not escaped correctly:

    /Producer (Œa6Sq©åðÇ9Å—ÙÒ°Çl¡ÿÝøVóVѪ!Õñ¶7(Þýä¡)
    

    There's an open bracket near the end of the string, which is not escaped nor balanced.

    The logic in these lines is faulty. The strings are escaped prior encryption which is simply wrong.