How to install phpwkhtmltopdf

江枫思渺然 提交于 2019-12-04 18:03:00

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

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