frengky/php-wkhtmltox

Simple wkhtmltox wrapper for PHP

v1.0.0 2020-04-26 03:54 UTC

This package is auto-updated.

Last update: 2024-04-26 13:09:29 UTC


README

Wkhtmltopdf is the top choice to render your html into a pdf or image. This package is a clean and easy wrapper for wkhtmltopdf and wkhtmltoimage. This package is working directly with wkhtmltopdf's process (stdin and stdout) without creating any temporary files.

Requirements

Install the Wkhtmltopdf binaries for your operating system, make sure wkhtmltopdf and wkhtmltoimage executable in your PATH.

Installation

Install the package via Composer

composer require frengky/php-wkhtmltox

Usage

Convert HTML string into a PDF file:

<?php
use Frengky\WkHtml\PDF;

$htmlString = 'Hello, <strong>World</strong>!'
$outputPath = PDF::fromHtml($contents)
           ->set('--page-size',  'A4') // the args to wkhtmltopdf
           ->set('--orientation',  'Portrait')
           ->saveAs('storage/files/test-success.pdf');

if ($result) {
   echo "Generated pdf output file path: " . $outputPath;
}

You can also working with stream. See PSR-7 StreamInterface and check out guzzle/psr7:

$htmlSource = Psr7\stream_for('Hello, <strong>World</strong>!');
$retval = PDF::fromHtml($htmlSource)
           ->set('--page-size',  'A4') // the args to wkhtmltopdf
           ->set('--orientation',  'Portrait')
           ->render(function(StreamInterface $output) {
				// Do something with the stream
				file_put_contents('output.pdf', $output->getContents());
			});

This is the recommended way if you working with large pdf file output.