justin423 has asked for the wisdom of the Perl Monks concerning the following question:

Can't figure out how to use WKHTMLTOPDF and getting a hash error

Here is the code with 3 different methods, but none work and seem to be relating to what is surrounding the filenames

use WKHTMLTOPDF; $filename='test.html'; $path='/temp/'; $inputfile="$path$filename"; $outputfilename='TEST'; $ext='.pdf'; $outputfile="$path$outputfilename$ext"; my $pdf = new WKHTMLTOPDF; $pdf->_input_file('/temp/TEST.html'); $pdf->_output_file('/temp/TEST.pdf'); $pdf->_input_file('$inputfile'); $pdf->_output_file('$outputfile'); $pdf->grayscale(1); $pdf->generate; #system ("cd C:\Program Files\wkhtmltopdf\bin"); #system ('start "C:\Program Files\wkhtmltopdf\bin\wkhtmltopdf $inputfi +le $outputfile"'); #system ('start "C:\Program Files\wkhtmltopdf\bin\wkhtmltopdf /temp/TE +ST.html /temp/TEST.pdf"');

Here is the error message

couldn't find command '/usr/bin/wkhtmltopdf' at C:/Strawberry/perl/sit +e/lib/MooseX/Role/Cmd.pm line 117. MooseX::Role::Cmd::run(WKHTMLTOPDF=HASH(0x6759d0), "/temp/TEST +.html", "/temp/TEST.pdf") called at C:/Strawberry/perl/site/lib/WKHTM +LTOPDF.pm line 645 WKHTMLTOPDF::generate(WKHTMLTOPDF=HASH(0x6759d0)) called at ht +mlpdf.pl line 15

Replies are listed 'Best First'.
Re: Help with WKHTMLTOPDF
by kcott (Archbishop) on Dec 08, 2023 at 00:03 UTC

    G'day justin423,

    "couldn't find command '/usr/bin/wkhtmltopdf' at ..."

    The documentation for WKHTMLTOPDF shows /usr/bin/wkhtmltopdf as the default. You appear to be on an MSWin system so it's unlikely that path would exist. It provides a bin_name to change that. I'd suggest starting there.

    You've also got a whole swag of other issues that I recommend you address. Here's a few that stand out; there could well be others.

    • Missing strict and warnings pragmata.
    • Use of package variables.
    • Indirect object notation.
    • _input_file() and _output_file() methods are repeated.
    • Non-interpolating single quotes around variables.

    — Ken

      You've also got a whole swag of other issues that I recommend you address ... strict and warnings ...

      Though I hope to be proved wrong, based on the op's most recent question pattern matching once (see also my reply), I don't like our chances of seeing these glaring issues addressed.

      👁️🍾👍🦟

      Same error even with it reinstalled in the Strawberry directory

      #!/usr/bin/perl use strict; use warnings; use WKHTMLTOPDF; my $filename='test.html'; my $path='/temp/'; my $inputfile="$path$filename"; my $outputfilename='test'; my $ext='.pdf'; my $outputfile="$path$outputfilename$ext"; my $pdf = new WKHTMLTOPDF; my $path1='/Strawberry/wkhtmltopdf/bin/'; $pdf->bin_name($path1); $pdf->_input_file('/temp/TEST.html'); $pdf->_output_file('/temp/TEST.pdf'); $pdf->_input_file($inputfile); $pdf->_output_file($outputfile); $pdf->grayscale(1); $pdf->generate; <code> couldn't find command '/Strawberry/wkhtmltopdf/bin/' at C:/Strawberry/ +perl/site/lib/MooseX/Role/Cmd.pm line 117. MooseX::Role::Cmd::run(WKHTMLTOPDF=HASH(0x6d59d0), "/temp/test +.html", "/temp/test.pdf") called at C:/Strawberry/perl/site/lib/WKHTM +LTOPDF.pm line 645 WKHTMLTOPDF::generate(WKHTMLTOPDF=HASH(0x6d59d0)) called at ht +mlpdf.pl line 20
        "couldn't find command '/Strawberry/wkhtmltopdf/bin/' ..."

        In "Re^2: Help with WKHTMLTOPDF" you identified the command as "C:\Strawberry\wkhtmltopdf\bin\wkhtmltopdf". Try using that.

        — Ken

        A reply falls below the community's threshold of quality. You may see it by logging in.
        > couldn't find command '/Strawberry/wkhtmltopdf/bin/' at C:/Strawberry/perl/...

        Do you see the difference at the start of those two paths?

        Cheers Rolf
        (addicted to the Perl Programming Language :)
        see Wikisyntax for the Monastery

Re: Help with WKHTMLTOPDF
by LanX (Saint) on Dec 07, 2023 at 23:45 UTC
    Did you install https://wkhtmltopdf.org/ and does it work from the command line?

    Looks to me like you are running it on windows but it defaults to a looking for the binary in the default Linux path: /usr/bin/wkhtmltopdf .

    And according to the POD of WKHTMLTOPDF the method bin_name must be used to specify the non-default path to the binary.

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    see Wikisyntax for the Monastery

      Yes, it works from a command prompt.
      C:\Strawberry\wkhtmltopdf\bin>wkhtmltopdf /temp/test.html /temp/test.p +df Loading pages (1/6) Counting pages (2/6) Resolving links (4/6) Loading headers and footers (5/6) Printing pages (6/6) Done Exit with code 1 due to network error: ProtocolUnknownError
Re: Help with WKHTMLTOPDF
by soonix (Chancellor) on Dec 08, 2023 at 14:13 UTC
    At a command prompt, enter
    dir /b \wkhtmltopdf.exe /s
    (with attention to the right number of forward and backward slashes), and you should see what to tell bin_name.

      C:\temp>dir /b \wkhtmltopdf.exe /s

      C:\Program Files\wkhtmltopdf\bin\wkhtmltopdf.exe

      C:\Strawberry\wkhtmltopdf\bin\wkhtmltopdf.exe

      Just an FYI. I tried the source code on a Ubuntu box with no bin lines at all and it comes back with the same error.

      Couldn't find command '/usr/bin/wkhtmltopdf' at /usr/local/share/perl/ +5.30.0/MooseX/Role/Cmd.pm line 117

      I think the bug is in the MooseX::Role module.