kriansa / h2p

H2P - Convert HTML to PDF using PHP and PhantomJS. Unmaintained.
https://garajau.com.br/projects/h2p-convert-html-files-to-pdf/
75 stars 35 forks source link

Generating corrupted 0KB PDF #19

Open Pepozzo opened 8 years ago

Pepozzo commented 8 years ago

I'm using this simple code to generate a PDF:

<?php
//include('autoload.php');
include('h2p/autoloader.php');

use H2P\Converter\PhantomJS;
use H2P\TempFile;

$filename = time();

$converter = new PhantomJS();
$input = new TempFile('<html>THIS IS A TEST</html>', 'html'); // Make sure the 2nd parameter is 'html'

// Convert destination accepts H2P\TempFile or string with the path to save the file
$converter->convert($input, $filename.'.pdf'); 
?>

The PDF is generated but it's 0 KB and Adobe Reader says it's corrupted when trying to open it.

My application is currently running on Windows 10 (via XAMPP) and I've put PhantomJS.exe (version 2.1) in h2p/bin/win32 folder.

No error are reported, even in console.

The PHP page that is running the script is placed in the root.

Any suggestion?

Clearmist commented 8 years ago

This same error existed back in 2014 according to an archived issue on this GitHub page. H2P seems to not work with PhantomJS version 2+. I am getting the same error using 2.1.1.

My suggestion is to roll back to Phantom JS 1.9 and test it then with the simple html example written in README.md.

kriansa commented 8 years ago

@Clearmist are you using Windows?

Clearmist commented 8 years ago

Hi @kriansa, I have discovered that one of the problems is you wrote the library to json encode the parameters, then you bash escape the string (which gets rid of double quotes), then convertor.js tries to decode the json. The problem is that the string is now invalid JSON! I solved this problem by base64 encoding the json string in PhantomJS.php's transform function then base64 decoding it in converter.js using args[1] = atob(args[1]);

Yes, I am using Windows.

I'm getting converter.js to spit back out lots of debug to find out why the output pdf is constantly blank. I've discovered that the input URI may be set incorrectly or unreadable because page.content is empty after PhantomJS tries to read the temporary .html file created by h2p. I can manually set page.content within the page.open function in converter.js and it writes correctly.

Clearmist commented 8 years ago

Okay, I fixed it. I'll issue a pull request within the hour. I fixed this 2 year old issue with pull request #20. Hopefully it works for you guys.

kriansa commented 8 years ago

Thank you @Clearmist !

Would you mind writing test cases for that issue?

Clearmist commented 8 years ago

Sorry, @kriansa. I am not experienced enough to know how to write a test case. Is that done with Travis CI?

kriansa commented 8 years ago

Hey @Clearmist,

Don't worry, I'll guide you through it. It's very straightforward, you just have to follow a testing pattern. We currently have some tests inside tests folder. Check it out: https://github.com/kriansa/h2p/tree/master/tests/H2P

Basically, you'll create one test that is supposed to fail in current master, and pass using your fix (i.e.: trying to convert anything that in a given moment gets double-encoded or something).

If you follow the four phase testing pattern, you won't find many trouble trouble while doing that, and you'll grow your engineering skills. Refer to: https://robots.thoughtbot.com/four-phase-test if you have questions about it.

By the way, Travis CI is an continuous integration tool, it handles release cycles (test, compile, build, release, etc). Whenever a new commit/PR gets pushed, it will run our test suite and checks whether it passes or not and will report back about the status of that build.

If you need further help, feel free to reach me at daniel at garajau.com.br.

hasekrx commented 8 years ago

@kriansa is it possible to change de margin of the body? i tried to see it in your instructions but i didnt find!

have a good day! Thankk you!

sohailhaider commented 7 years ago

Hi @kriansa , I'm still facing the same issue (on windows). Even I have used pull request of @Clearmist but still no good. I have tried PhantomJS version 1.9.8 but till it keeps loading and pdf is 0kb of size.