Coder Social home page Coder Social logo

Parse Error in windows about h2p HOT 12 OPEN

kriansa avatar kriansa commented on August 19, 2024
Parse Error in windows

from h2p.

Comments (12)

RaXaR avatar RaXaR commented on August 19, 2024

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().

from h2p.

kriansa avatar kriansa commented on August 19, 2024

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

from h2p.

RaXaR avatar RaXaR commented on August 19, 2024

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.

from h2p.

kriansa avatar kriansa commented on August 19, 2024

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).

from h2p.

RaXaR avatar RaXaR commented on August 19, 2024

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

from h2p.

RaXaR avatar RaXaR commented on August 19, 2024

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');

from h2p.

RaXaR avatar RaXaR commented on August 19, 2024

Note: Updated previous comment with a few fixes.

from h2p.

Seb33300 avatar Seb33300 commented on August 19, 2024

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:///

from h2p.

kriansa avatar kriansa commented on August 19, 2024

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)

from h2p.

Seb33300 avatar Seb33300 commented on August 19, 2024

The PR fixed the issue with file://.

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

from h2p.

kriansa avatar kriansa commented on August 19, 2024

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

from h2p.

Seb33300 avatar Seb33300 commented on August 19, 2024

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.

from h2p.

Related Issues (13)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.