dealfonso / sapp

Simple and Agnostic PDF Document Parser in PHP - sign PDF docs using PHP
GNU Lesser General Public License v3.0
115 stars 30 forks source link

Sign pdf generated by Mpdf php lib #19

Closed raimon-segura closed 2 years ago

raimon-segura commented 2 years ago

Hi!,

I'm trying to sign a pdf generated with Mpdf php library:

The set_signature_certificate method increases the filesize but the file doesn't show any certificate, I've been checking with "libreOffice Draw". Also, I've tried to rebuild the pdf with sapp before set signature and sign the pdf using terminal pdfsign.php script without any luck.

The only thing that works is to convert using pdftk after generate it with mpdf, and then sign with pdfsign.php script, but this is not a good workflow for me, ideally it should work only with php.

I'm attaching several example pdf, hope this helps to find the issue:

simple_pdf_not_serversigned.pdf simple_pdf_not_serversigned_pdftk.pdf simple_pdf_not_serversigned_pdftk_terminalsigned_OKSIGNED.pdf simple_pdf_not_serversigned_terminalsigned.pdf simple_pdf_rebuilded_and_serversigned.pdf simple_pdf_rebuilded_and_serversigned_pdftk_INVALIDSIGNED.pdf simple_pdf_serversigned.pdf simple_pdf_serversigned_pdftk_INVALIDSIGNED.pdf

Many Thanks!

raimon-segura commented 2 years ago

@dealfonso Any hint? ...maybe I can find the bug by myself, but not sure where to start :D

...thanks!

dealfonso commented 2 years ago

Hi,

I'll try to check it this week.

dealfonso commented 2 years ago

I have just inspected your docs... and if you dump them (e.g. using cat), I can see some problems:

In doc _simple_pdfserversigned.pdf, after the document ends, I can see the next HTML code:

<pre>An Error occurred while handling another error:
yii\web\HeadersAlreadySentException: Headers already sent in /app/backend/controllers/StudentController.php on line 1028. in /app/vendor/yiisoft/yii2/web/Response.php:368
Stack trace:
#0 /app/vendor/yiisoft/yii2/web/Response.php(341): yii\web\Response-&gt;sendHeaders()
#1 /app/vendor/yiisoft/yii2/web/ErrorHandler.php(136): yii\web\Response-&gt;send()
#2 /app/vendor/yiisoft/yii2/base/ErrorHandler.php(135): yii\web\ErrorHandler-&gt;renderException(Object(yii\web\HeadersAlreadySentException))
#3 [internal function]: yii\base\ErrorHandler-&gt;handleException(Object(yii\web\HeadersAlreadySentException))
#4 {main}
Previous exception:
yii\web\HeadersAlreadySentException: Headers already sent in /app/backend/controllers/StudentController.php on line 1028. in /app/vendor/yiisoft/yii2/web/Response.php:368
Stack trace:
#0 /app/vendor/yiisoft/yii2/web/Response.php(341): yii\web\Response-&gt;sendHeaders()
#1 /app/vendor/yiisoft/yii2/base/Application.php(390): yii\web\Response-&gt;send()
#2 /app/backend/web/index.php(68): yii\base\Application-&gt;run()
#3 {main}</pre>

In the middle of document _simple_pdf_not_serversignedterminalsigned.pdf, I see the next HTML code:

<pre>An Error occurred while handling another error:
yii\web\HeadersAlreadySentException: Headers already sent in /app/backend/controllers/StudentController.php on line 1028. in /app/vendor/yiisoft/yii2/web/Response.php:368
Stack trace:
#0 /app/vendor/yiisoft/yii2/web/Response.php(341): yii\web\Response-&gt;sendHeaders()
#1 /app/vendor/yiisoft/yii2/web/ErrorHandler.php(136): yii\web\Response-&gt;send()
#2 /app/vendor/yiisoft/yii2/base/ErrorHandler.php(135): yii\web\ErrorHandler-&gt;renderException(Object(yii\web\HeadersAlreadySentException))
#3 [internal function]: yii\base\ErrorHandler-&gt;handleException(Object(yii\web\HeadersAlreadySentException))
#4 {main}
Previous exception:
yii\web\HeadersAlreadySentException: Headers already sent in /app/backend/controllers/StudentController.php on line 1028. in /app/vendor/yiisoft/yii2/web/Response.php:368
Stack trace:
#0 /app/vendor/yiisoft/yii2/web/Response.php(341): yii\web\Response-&gt;sendHeaders()
#1 /app/vendor/yiisoft/yii2/base/Application.php(390): yii\web\Response-&gt;send()
#2 /app/backend/web/index.php(68): yii\base\Application-&gt;run()
#3 {main}</pre>8 0 obj

This happens because the original doc contains that code, thus making the final doc invalid.

Doc _simple_pdf_rebuilded_and_serversigned_pdftkINVALIDSIGNED.pdf does not seem to have been signed at all.

Please check the docs and try again.

raimon-segura commented 2 years ago

Hi Carlos!,

You are right! ...and the solution is very simple, it needs an "exit()" just after the "echo"

... header("Content-type:application/pdf"); header("Content-Disposition:attachment;filename=$pdf_filename"); echo $obj->to_pdf_file_s(true); exit();

Many thanks for your help!