Download the PHP package mikehaertl/phpwkhtmltopdf without Composer

On this page you can find all versions of the php package mikehaertl/phpwkhtmltopdf. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package phpwkhtmltopdf

PHP WkHtmlToPdf

GitHub Tests Packagist Version Packagist Downloads GitHub license Packagist PHP Version Support

PHP WkHtmlToPdf provides a simple and clean interface to ease PDF and image creation with wkhtmltopdf. The wkhtmltopdf and - optionally - wkhtmltoimage command must be installed and working on your system. See the section below for details.

For Windows systems make sure to set the path to wkhtmltopdf.exe in the binary option. Alternatively you can add the wkhtmltopdf "bin" directory to the system PATH variable to allow wkhtmltopdf command available to Windows CMD.

Installation

Install the package through composer:

Make sure, that you include the composer autoloader somewhere in your codebase.

Examples

Single page PDF

Multi page PDF with Toc and Cover page

Creating an image

Setting options

The wkhtmltopdf shell command accepts different types of options:

Please see wkhtmltopdf -H for a full explanation. All options are passed as array, for example:

Options can be passed to several methods for PDFs:

Note, that you can also use page options in the global PDF options. wkhtmltopdf will apply them to all pages unless you override them when you add a page.

For wkhtmltoimage there's only one set of options:

Wrapper options

The wrapper itself is configured by the following special options that can be passed to the constructor, set as object properties or via setOptions():

In addition to the binary, commandOptions, tmpDir and ignoreWarnings options above, the Image class also has a type option:

commandOptions can be used to set environment variables for wkhtmltopdf. For example, if you want to pass UTF-8 encoded arguments, you may have to set the LANG environment variable.

Passing strings

Some options like header-html usually expect a URL or a filename. With our library you can also pass a string. The class will try to detect if the argument is a URL, a filename or some HTML or XML content. To make detection easier you can surround your content in <html> tag.

If this doesn't work correctly you can also pass an instance of our File helper as a last resort:

Error handling

send(), saveAs() and toString() will return false on error. In this case the detailed error message is available from getError():

Known Issues

Use under Windows

If you use double quotes (") or percent signs (%) as option values, they may get converted to spaces. In this case you can disable argument escaping in the command. There are also two interesting options to proc_open() that you may want to use on Windows:

But then you have to take care of proper argument escaping yourself. In some cases it may be neccessary to surround your argument values with extra double quotes.

I also found that some options don't work on Windows (tested with wkhtmltopdf 0.11 rc2), like the user-style-sheet option used in the example below.

Download Problems

There have been many reports about corrupted PDFs or images when using send(). They are often caused by the webserver (Apache, Nginx, ...) performing additional compression. This will mess up the Content-Length header which is added by this library. It's useful to let the browser show a progress bar.

To fix this there are two options:

  1. Exclude the download URL from compression in your Webserver. For example if your script is called pdf.php then for mod_deflate in Apache you could try to add this to your configuration:

    For Nginx there are similar solutions to disable gzip for a specific location.

  2. Suppress the Content-Length header when you send a file (available since 2.5.0):

Installation of wkhtmltopdf

It's recommended that you download the latest wkhtmltopdf from their website:

http://wkhtmltopdf.org/downloads.html

These versions should run out of the box.

If for some reason you can't do so, you may run into an issue with the dynamically linked version of wkhtmltopdf. This is what you get for example on Ubuntu 12.04 LTS if you install the wkhtmltopdf package. It will work, but to use all features it requires an X server which is usually not available on headless webservers.

We therefore provide two Xvfb based workarounds. You can either use

Both require the Xvfb package to be installed on the system and both also have some drawbacks.

Built in Xvfb support

This wraps each call to wkhtmltopdf with xvfb-run. xvfb-run will run any given command in a X environment without all the overhead of a full X session. The drawback with this solution is, that there's still a new session fired up for each an every PDF you create, which will create quite some extra load on your CPU. So this setup is only recommended for low frequency sites.

To use the built in support you have to set enableXvfb in the commandOptions. There are also some options you can set.

Standalone Xvfb

It's better to start a Xvfb process once and reuse it for all your PHP requests (thanks to Larry Williamson for the original idea). This requires that you have root access to your machine as you have to add a startup script for that process. We have provided an example init script for Ubuntu (thanks eusonlito). You can put it to /etc/init.d/xvfb and add it to your startup files with update-rc.d xvfb defaults 10.

If your system is based on systemd this config should help (thanks nkm).

If your Xvfb process is running, you just have to tell the class to use this X display for rendering. This is done via an environment variable.

Full example

For me wkhtmltopdf seems to create best results with smart shrinking turned off. But then I had scaling issues which went away after I set all margins to zero and instead added the margins through CSS. You can also use cm or in in CSS as this is more apropriate for print styles.

demo.html

pdf.css

Links

Also check out my php-pdftk wrapper around pdftk which brings the full power of pdftk to PHP.


All versions of phpwkhtmltopdf with dependencies

PHP Build Version
Package Version
Requires php Version >=5.0.0
mikehaertl/php-tmpfile Version ^1.2.1
mikehaertl/php-shellcommand Version ^1.5.0
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package mikehaertl/phpwkhtmltopdf contains the following files

Loading the files please wait ....