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

Parse Error in windows #14

Open
samiksha-singla opened this issue Feb 19, 2015 · 12 comments
Open

Parse Error in windows #14

samiksha-singla opened this issue Feb 19, 2015 · 12 comments
Labels

Comments

@samiksha-singla
Copy link

Library is creatin following command on window
"C:\www\myproject\vendor\kriansa\h2p\bin/win32/phantomjs.exe" "C:\www\myproject\vendor\kriansa\h2p\bin/converter.js" "{ destination : C:\Windows\TEMP\14b6e6edc209b6e731bdbc79976a9430438409ae.tmp , request :{ uri : C:\Windows\TEMP\127ef8f78c335d9af694a9cef0423541dd17a256.html , method : GET }, orientation : Portrait , format : A3 , zoomFactor :1, allowParseCustomFooter :false, allowParseCustomHeader :false, border : 1cm , header :null, footer :null}"
When I run this command it is giving me parse error . Seems json is not getting created properly

@RaXaR
Copy link

RaXaR commented Nov 13, 2015

I also got this error. I took the JSON string you pasted above tried it over at http://codebeautify.org/jsonviewer. I had to convert it to the below to get it to validate:

{ 
    "destination": "C:\\\\Windows\\\\TEMP\\\\14b6e6edc209b6e731bdbc79976a9430438409ae.tmp", 
    "request": { 
        "uri": "C:\\\\Windows\\\\TEMP\\\\127ef8f78c335d9af694a9cef0423541dd17a256.html", 
        "method": "GET"
    }, 
    "orientation": "Portrait", 
    "format": "A3", 
    "zoomFactor": 1, 
    "allowParseCustomFooter": false, 
    "allowParseCustomHeader":false, 
    "border": "1cm", 
    "header": null, 
    "footer": null
}

Not sure yet if I can transfer this to the H2P library

EDIT: Had to update the backslashes to \\ to work with JSON.parse().

@kriansa
Copy link
Owner

kriansa commented Nov 13, 2015

Hey @RaXaR , can you test setting the temp folder to a different one?

@RaXaR
Copy link

RaXaR commented Nov 13, 2015

Hi Yeah, i tried using a folder inside the webroot but the same error appears:

Error while executing PhantomJS: "SyntaxError: JSON Parse error: Expected '}'" 

To my eye it looks like an issue with escapeshellarg() being used in PhatomJS::transform() and then the parsing inside the converter.js at JSON.parse().

However, I can't figure out how to debug inside coverter.js so that I can see what strings it receives.

@kriansa
Copy link
Owner

kriansa commented Nov 13, 2015

You can try using console.log() inside converter.js and fetching its
result inside PHP, can you do that?

Daniel Pereira

2015-11-13 10:30 GMT-02:00 RaXaR [email protected]:

Hi Yeah, i tried using a folder inside the webroot but the same error
appears:

Error while executing PhantomJS: "SyntaxError: JSON Parse error: Expected '}'"

To my eye it looks like an issue with escapeshellarg() being used in
PhatomJS::transform() and then the parsing inside the converter.js at
JSON.parse().

However, I can't figure out how to debug inside coverter.js so that I can
see what strings it receives.


Reply to this email directly or view it on GitHub
#14 (comment).

@RaXaR
Copy link

RaXaR commented Nov 13, 2015

Nope, I don't know how to get the output of console.log to PHP?

@RaXaR
Copy link

RaXaR commented Nov 13, 2015

Ok I resolved the parse error on windows with:

// /Converter/PhantomJS.php
    protected function transform(Request $origin, $destination)
    {
        $request = self::fixRequestKeyNames($origin);

        $args = array(
            'destination' => $destination,
            'request' => $request,
        ) + $this->options;

        // If on Windows then do not use escapeshellarg();
        $os = php_uname('s');
        switch ($os) {
            case 'Windows NT':
                $json = json_encode($args);
                $json = str_replace('"', '\"', $json);
                $json = '"' . $json . '"';
                $command = $this->getBinPath() . ' ' . $json;
                break;
            default:
                $command = $this->getBinPath() . ' ' . escapeshellarg(json_encode($args));
        }

        $result = json_decode(trim(shell_exec($command)));

        if (!$result->success) {
            throw new Exception('Error while executing PhantomJS: "' . $result->response . '"');
        }

        return true;
    }

I tested with:

            $pdfDir = 'C:' . DIRECTORY_SEPARATOR . 'wamp' . DIRECTORY_SEPARATOR . 'www' . DIRECTORY_SEPARATOR;
            $converter = new PhantomJS();
            $input = 'http://www.google.com/';
            // Convert destination accepts H2P\TempFile or string with the path to save the file
            $converter->convert($input, $pdfDir . 'page.pdf');

When I use new Tempfile() for input then I get:

Error while executing PhantomJS: "Unable to access the URI! (Make sure you're using a .html extension if you're trying to use a local file)"

Tested with:

            $pdfDir = 'C:' . DIRECTORY_SEPARATOR . 'wamp' . DIRECTORY_SEPARATOR . 'www' . DIRECTORY_SEPARATOR;
            $converter = new PhantomJS();
            $input = new TempFile('<b>test string to pdf</b>', '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, $pdfDir . 'page.pdf');

@RaXaR
Copy link

RaXaR commented Nov 13, 2015

Note: Updated previous comment with a few fixes.

@Seb33300
Copy link

I had aa similar problem today on windows.

I had to use your updates transform() function and to had this fix:
$json = str_replace('C:', 'file:///c:', $json);

PhantomJS is not able to access fils on filesystem if url is not prefixed by file:///

@kriansa
Copy link
Owner

kriansa commented Mar 24, 2016

I've just merged a PR that fixes that issues on Windows.

Can you guys test using master branch? (or dev-master for those using composer)

@Seb33300
Copy link

The PR fixed the issue with file://.

But not the original issue about the parse Error in windows.

@kriansa
Copy link
Owner

kriansa commented Mar 24, 2016

I thought that the parsing error was directly related to the file:// issue - my bad.

@Seb33300
Copy link

No, on windows, the parsing error is related to the escapeshellarg function:

On Windows, escapeshellarg() instead removes percent signs, replaces double quotes with spaces and adds double quotes around the string.

So the command line is generated with an invalid json.

@kriansa kriansa added the bug label Apr 29, 2016
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

4 participants