spipu / html2pdf

OFFICIAL PROJECT | HTML to PDF converter written in PHP
http://html2pdf.fr/en/default
Open Software License 3.0
1.67k stars 745 forks source link

[PDF] Generating damaged or corrupted pdf #678

Open MarcelloDM opened 2 years ago

MarcelloDM commented 2 years ago

I get the error by by running example scripts .ps1 and .sh to test located in the module path ..\vendor\spipu\html2pdf\test. _

Adobe reader could not open "file.pdf" because it is either not a support file type or because the file has been damaged (for example, it was sent as email attachment and was'nt correctly decoded). _

Any other got this problem? Thanks in advance.

MarcelloDM commented 2 years ago

[PROBLEM] Windows Path Encoding Error

After installing by Composer, I tried to run the script generate.sh by shell/dos. The execution failed. PDFs are generate but not correctly encoded on opening as follow (i.e. about.php):

image

[SOLVED]

1. Path location change for running script by shell/dos/pshell

Every test php file into the examples directory references to the autoload.php and all *.php files as follow:

require_once dirname(__FILE__).'/../vendor/autoload.php'; include dirname(__FILE__).'/res/about.php';

We need to change path locations in every php script file by using escape characters " \\ ":

require_once dirname(__FILE__, 5).'\\vendor\\autoload.php';

include dirname(__FILE__).'\\res\\*.php';

2. Adding statement $html2pdf->pdf->AddPage();

We need to add $html2pdf->pdf->AddPage(); to generation pdf script file before writeHTML() method.

After changing, all PDFs works.

It's because...

The method dirname(__FILE__)returns a simple string and Windows does not consider "/" as path separator among files The PHP doc here. image