Welcome to a user-friendly guide that will walk you through the process of using the Paper library for converting HTML to PDF files. This hassle-free abstraction library enables you to produce high-quality PDFs quickly and efficiently, offering a variety of drivers and asynchronous options.
Installation: Getting Started
First things first, let’s get Paper up and running. You can install it via Composer with the following command:
composer require asyncphppaper
For optimal results, it’s also recommended to install the following utilities:
- Prince – Great for modern CSS and JavaScript support.
- WKHTMLtoPDF – Another option for HTML to PDF conversion.
- diff-pdf – Needed to run tests.
Note: If you don’t have the latter installed, tests won’t run, and missing the former will skip relevant tests, making some drivers unusable.
Usage: Converting HTML to PDF
Using the Paper library is quite straightforward. You can either leverage the driver directly or conveniently use the factory to create pre-configured drivers. Let’s explore both methods.
1. Direct Usage of the Driver
Here’s how you can create and use a driver:
php
use AsyncInteropLoop;
use AsyncPHPPaperDriverDomDriver;
use AsyncPHPPaperRunnerAmpRunner;
Loop::execute(Ampwrap(function() use ($sample) {
$driver = new DomDriver();
$runner = new AmpRunner();
$promise = $driver
->html($sample)
->size(A4)
->orientation(portrait)
->dpi(300)
->render($runner);
$results = yield $promise;
});
Think of this process like an assembly line in a factory. The HTML sample is your raw material, and the driver is the assembly line that meticulously crafts it into a polished PDF product. The configuration parameters like size and orientation are akin to quality control settings ensuring the final product meets your requirements.
2. Using the Factory for Pre-configured Drivers
If you prefer a simplified approach, using the factory can save you time:
php
use AsyncPHPPaperFactory;
$config = [
'driver' => 'dom',
'dom' => [
'options' => [
'fontDir' => __DIR__ . '/fonts',
'https://github.com/dompdf/dompdf/blob/master/src/Options.php'
],
],
'prince' => [
'binary' => '/opt/prince/bin/prince',
'tempPath' => __DIR__,
'options' => [
'--no-compress',
'--http-timeout=10',
'https://www.princexml.com/doc/command-line#command-line'
],
],
'webkit' => [
'binary' => '/usr/local/bin/wkhtmltopdf',
'tempPath' => __DIR__,
'options' => [
'--grayscale',
'--javascript-delay=500',
'http://wkhtmltopdf.org/usage/wkhtmltopdf.txt'
],
],
'runner' => 'amp',
];
$factory = new Factory();
$driver = $factory->createDriver($config);
$runner = $factory->createRunner($config);
yield $driver->html($sample)->render($runner);
This method is like using a customizable template to prepare your PDF, allowing you to fine-tune various aspects without starting from scratch. You just need to fill in your specifics, and the factory configures everything for you.
Drivers: A Quick Overview
Here are the available drivers to assist with your PDF conversion:
- DOMPDF
- Command-line utilities: No
- Supports modern CSS: No
- Supports modern JS: No
- Produces vector files: Yes
- Open + free: Yes
- WKHTMLtoPDF
- Command-line utilities: Yes
- Supports modern CSS: Yes
- Supports modern JS: Yes
- Produces vector files: Yes
- Open + free: Yes
- Prince
- Command-line utilities: Yes
- Supports modern CSS: Yes
- Supports modern JS: Yes
- Produces vector files: Yes
- Open + free: No
Troubleshooting: Common Issues and Solutions
If you encounter any issues while using the Paper library, consider the following troubleshooting tips:
- Ensure that all required libraries are properly installed and up-to-date.
- Verify that the paths to the command-line utilities are correct in your configuration.
- If rendering fails, check the sample HTML content for any unsupported tags or styles.
For more insights, updates, or to collaborate on AI development projects, stay connected with fxis.ai.
Final Thoughts
At fxis.ai, we believe that such advancements are crucial for the future of AI, as they enable more comprehensive and effective solutions. Our team is continually exploring new methodologies to push the envelope in artificial intelligence, ensuring that our clients benefit from the latest technological innovations.