How to install phpwkhtmltopdf

不问归期 提交于 2019-12-09 22:19:49

问题


I am trying to install phpwkhtmltopdf on shared server (webhotel). The server has already composer installed, and I have SSH to server. But I am not too familiar with composer so I might be doing someting very basic wrong here..

So, I have downloaded the zip file from: https://github.com/mikehaertl/phpwkhtmltopdf and extracted the files on it to server to directory named as phpwkhtmltopdf. Then, opening SSH cd-ing to that dir and running composer require mikehaertl/phpwkhtmltopdf but I only get question marks to beginning of line. For the question marks I read that it could be a problem of detect unicode, so I placed to htaccess: php_flag detect_unicode Off and it seems that it is Off now locally.

But there is still the question marks and php wkhtmltopdf does not get installed. How to get it installed?


回答1:


The problem is that you are fetching the package mikehaertl/phpwkhtmltopdf two times:

  • firstly, you fetched the zip and extracted it (manual installation).
  • then, you ran Composer to require it (installation via Composer).

Please decide how you want to install the package!


When you want to install the package with Composer, you just need to run composer require mikehaertl/phpwkhtmltopdf in a clean project folder.

Composer will then fetch the package and place it into the /vendor folder.

That's it.


Now, in order to use it you need two things:

  1. you need to include Composers Autoloader during the bootstrap of your project. This enables that the Autoloader will load the library, when you access one the package/library classes.

    // Register Composer Autoloader
    
    define('VENDOR_DIR', __DIR__ . '\vendor' . DS);
    
    if (!is_file(VENDOR_DIR . 'autoload.php')) {
        throw new \RuntimeException(
            '[Error] Bootstrap: Could not find "vendor/autoload.php".' . PHP_EOL .
            'Did you forget to run "composer install --dev"?' . PHP_EOL
        );
    }
    
    require VENDOR_DIR . 'autoload.php';
    
  2. Well, finally, you need to code something using the library:

    use mikehaertl\wkhtmlto\Pdf;
    
    $pdf = new Pdf('/path/to/page.html');
    
    if (!$pdf->saveAs('/path/to/page.pdf')) {
        echo $pdf->getError();
    }
    

Uhm, and you need the wkhtmltopdf binary... but i guess thats not the problem.


These are my steps:

  1. i downloaded wkhtmltopdf http://download.gna.org/wkhtmltopdf/0.12/0.12.3.2/wkhtmltox-0.12.3.2_msvc2013-win64.exe
  2. and installed it into c:\program files\wkhtmltopdf
  3. now the executable resides in c:\program files\wkhtmltopdf\bin

  4. i created the folder pdf-test

  5. i ran the command composer require mikehaertl/phpwkhtmltopdf on the CLI
  6. i created a file makepdf.php with the following content:

    /**
     * Register Composer Autloader
     */
    define('VENDOR_DIR', __DIR__ . '\vendor' . DIRECTORY_SEPARATOR);
    
    if (!is_file(VENDOR_DIR . 'autoload.php')) {
        throw new \RuntimeException(
            '[Error] Bootstrap: Could not find "vendor/autoload.php".' . PHP_EOL .
            'Did you forget to run "composer install --dev"?' . PHP_EOL
        );
    }
    
    require VENDOR_DIR . 'autoload.php';
    
    /**
     * Use library
     */
    
    use mikehaertl\wkhtmlto\Pdf;
    
    
    $pdf = new Pdf(array(
        'binary' => 'C:\Program Files\wkhtmltopdf\bin\wkhtmltopdf.exe',
        'ignoreWarnings' => true,
        'commandOptions' => array(
            'procEnv' => array(
                // Check the output of 'locale' on your system to find supported languages
                'LANG' => 'en_US.utf-8',
            ),
            'escapeArgs' => false,
            'procOptions' => array(
                // This will bypass the cmd.exe which seems to be recommended on Windows
                'bypass_shell' => true,
                // Also worth a try if you get unexplainable errors
                'suppress_errors' => true,
            ),
        ),
    ));
    
    $pdf->addPage('<html>
    <head>
    </head>
    <body>
    
        <div id="print-area">
            <div id="header">
                This is an example header.
            </div>
            <div id="content">
                <h1>Demo</h1>
                <p>This is example content</p>
            </div>
            <div id="footer">
                This is an example footer.
            </div>
        </div>
    
    </body>
    </html>');
    
    if (!$pdf->saveAs('page.pdf')) {
        echo $pdf->getError();
    }
    
  7. then i ran php makepdf.php

  8. finally, the file page.pdf was generated

And that's it... :)



来源:https://stackoverflow.com/questions/37568675/how-to-install-phpwkhtmltopdf

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!