Skip to content
This repository has been archived by the owner on Nov 24, 2020. It is now read-only.

Generating corrupted 0KB PDF #19

Open
Pepozzo opened this issue Apr 26, 2016 · 9 comments
Open

Generating corrupted 0KB PDF #19

Pepozzo opened this issue Apr 26, 2016 · 9 comments

Comments

@Pepozzo
Copy link

Pepozzo commented Apr 26, 2016

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
Copy link

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
Copy link
Owner

kriansa commented Apr 28, 2016

@Clearmist are you using Windows?

@Clearmist
Copy link

Clearmist commented Apr 28, 2016

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
Copy link

Clearmist commented Apr 28, 2016

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
Copy link
Owner

kriansa commented Apr 29, 2016

Thank you @Clearmist !

Would you mind writing test cases for that issue?

@Clearmist
Copy link

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

@kriansa
Copy link
Owner

kriansa commented May 4, 2016

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
Copy link

hasekrx commented Jun 2, 2016

@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
Copy link

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.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants