Looking for more information on how to do PHP the right way? Check out PHP: The Right Way

Alex Vanderbist:
Fixing Imagick's “not authorized” exception
Nov 07, 2018 @ 16:48:40

Alex Vanderbist has a recent post to his site showing how to fix an issue with Imagick and the "not authorized" exception that can happen when processing PDF files.

Over the last few days we've had a couple of issues with Imagick and processing PDFs on our servers. As it turns out, these issues are caused by automatic security updates. Let's look into the issue and its solution.

The post starts with an example of the errors they've been seeing with the "not authorized" message in their Bugsnag logs. As they traced the problem they saw it was happening on older codebases too, not just new ones. Finally they tracked down the culprit: a recent security update to Imagick that added extra policies for PDFs that need to be configured for it to work as before. They provide an example of what should be added to your policy.xml file and how to bulk-patch your systems via Ansible.

tagged: tutorial processing pdf imagick policy fix

Link: https://alexvanderbist.com/posts/2018/fixing-imagick-error-unauthorized

Oscar Merida:
Creating ZIP files and streaming the response with Silex
Jan 19, 2018 @ 17:28:44

On his site Oscar Merida has shared a post showing how to create a ZIP file and stream the response in a Silex-based application using the DOMPDF library and PHP's ZipArchive functionality.

Creating a zip file is easy and Rob Allen has you covered on that front to get you started. I needed to do this to create a zip file of PDF files generated by Dompdf. It turns out, you can add files to a ZipArchive object without actually creating the file if you can get the contents as a string.

The post includes the code to create the DOMPDF instance, load the HTML and render the result. From there the ZipArchive takes over and creates the archive from the PDF result. Finally the code takes the ZIP archive as input and pushes it out with a custom header to tell the browser to download it and streams back the raw ZIP contents.

tagged: tutorial silex dompdf pdf zip archive silex stream download

Link: http://oscarm.org/2018/01/stream-a-zip-file-via-silex/

Pineco.de:
Simple PDF Rendering with Laravel
Dec 06, 2017 @ 16:48:37

On the Pineco.de site they've posted a tutorial showing you how to use the Laravel framework to generate PDFs with content provided from the system. To accomplish the content-to-PDF conversion, they make use of the dompdf library.

PDF generation is a core feature when our app contains invoicing or services that require a downloadable version of an information schema. Like in many other situations, a simple solution would do it instead of a full-featured package. Let’s see how to render PDFs easily.

Once the package is pulled in via Composer they add in a custom configuration to handle some of the basic settings including the paths to the fonts, the paper size and what DPI to use to render the final product. With that in place the post starts in on creating the custom PDF service, extending the Dompdf class and adding in a bit more Laravel-specific functionality for the service. After explaining this code they move on to updating the container to include the service via a provider and show the creation of a simple PDF controller. This controller uses the service to generate PDF output from the data in their invoice object and stream the results directly back to the client.

tagged: pdf rendering laravel dompdf tutorial service provider

Link: https://pineco.de/simple-pdf-rendering-laravel/

SitePoint PHP Blog:
Writing Async Libraries – Let’s Convert HTML to PDF
Feb 21, 2017 @ 15:58:05

The SitePoint PHP blog has another tutorial posted from author Christopher Pitt looking at writing async libraries with PHP. In this particular article he focuses on just one of many tasks an asynchronous library could perform: converting HTML to PDF documents.

I can barely remember a conference where the topic of asynchronous PHP wasn’t discussed. I am pleased that it’s so frequently spoken about these days. There’s a secret these speakers aren’t telling, though: "Making asynchronous servers, resolving domain names, interacting with file systems: these are the easy things. Making your own asynchronous libraries is hard. And it’s where you spend most of your time!"

The reason those easy things are easy is because they were the proof of concept – to make async PHP competitive with NodeJS. [...] Today, we’re going to look at a few ways to make your application code work well in an asynchronous architecture. Fret not – your code can still work in a synchronous architecture, so you don’t have to give anything up to learn this new skill. Apart from a bit of time…

He starts with some theory about things in the async world including callbacks, promises and what they might look like in PHP-land. He then starts in on the creation of the PDF files, creating a "Driver" class to handle some of the logic and using the Dompdf library to do the heavy lifting (the conversion from HTML to PDF). He walks through the code required for this class then moves on to the code, using the Amp project, to handle the async operations. He then creates a simple set of web accessible endpoints that call the Driver class with some basic attributes and performing the conversion. He ends the post talking about porting the parallel driver to other systems (such as ReactPHP) and a few simple steps if you need to move back to the synchronous world.

tagged: asynchronous conversion dompdf html pdf tutorial amp

Link: https://www.sitepoint.com/writing-async-libraries-lets-convert-html-to-pdf/

TutsPlus.com:
What Are Laravel 5.0 Facades?
Feb 23, 2016 @ 18:22:49

The TutsPlus.com site has posted a tutorial for those either new to the Laravel framework or wanting to get started a bit easier. In this new post they talk about the "facades" the framework makes wide use of - what they are and how they work.

The facade is very similar to the adapter and decorator patterns. The adapter acts like a bridge between two interfaces which are not compatible, while the decorator is more complex and used for dynamically changing the way objects behave.

[...] Sweet syntax, which Laravel uses, makes writing code cleaner and easier to understand. Laravel facades are actually the syntactic sugar for service location.

He uses the Cache facade in his introduction, showing where the various parts of it are defined and how it hooks in to the framework's functionality. With the basics out of the way he then shows how to create a custom facade instance: a simple check to see if the file name provided is a PDF or not.

tagged: laravel facade introduction tutorial custom pdf file

Link: http://code.tutsplus.com/tutorials/what-are-laravel-50-facades--cms-25347

SitePoint PHP Blog:
Filling out PDF Forms with PDFtk and PHP
Dec 03, 2015 @ 18:35:08

On the SitePoint PHP blog they've posted a tutorial from Reza Lavaryan showing how to fill out PDF forms with PDFtk and a bit of PHP on the side. PDFtk is a toolkit that provides easier handling for PDF files and content.

PDF files are one of the most common ways of sharing documents online. Whether we need to pass our clients’ documents to third-party service providers like banks or insurance companies, or just to send a CV to an employer, using a PDF document is frequently the first option.

PDF files can transfer plain/formatted text, images, hyperlinks, and even fillable forms. In this tutorial, we’re going to see how we can fill out PDF forms using PHP and a great PDF manipulation tool called PDFtk Server.

They walk you through the installation of the pdftk tool on a Homestead (Improved) virtual machine and give an overview of how it works and what makes up a PDF file. He then shows how to use PHP's exec function to make calls to the pdftk command line tool and fill in a form by editing the raw PDF content. To make it even easier he shows how to create a simple wrapper class around this that takes in the data to populate, the PDF to use and spits back out the download of the new PDF. The post ends with a quick example of extracting the current fields from the PDF, getting back more human readable output.

tagged: pdf form pdftk tutorial library populate data

Link: http://www.sitepoint.com/filling-pdf-forms-pdftk-php/

SitePoint PHP Blog:
Effective PDF Generation in Drupal
Feb 03, 2015 @ 15:43:53

On the SitePoint PHP blog today there's a new post showing you how to create PDFs in a Drupal-based site making use of the Print module for the creation and formatting.

DF generation takes a slight change of mindset. As web developers, we have spent a lot of time convincing designers from a print background to stop producing pixel perfect designs that will be difficult to reproduce on the web. If you want to introduce PDF generation or any form of high designed print output, then we need to relearn some of our old skills we left behind. The nature of print means that it is precise and often needs pixel (or millimeter) perfect design.

He'd initially thought that the Views PDF module would be the natural choice, but after finding some unwanted dependencies, opted for the Print module instead. The Print module only creates the formatted output, though. This is then passed off to wkhtmltopdf to convert into final PDF form. He walks you through the configuration for the Print module and how to create some of the basic HTML structure for the resulting output. Next up is the addition of some styling and the process for exporting the HTML output over to wkhtmltopdf for handling. Output samples are included to help illustrate the final result.

tagged: pdf generation drupal tutorial views print wkhtmltopdf convert

Link: http://www.sitepoint.com/effective-pdf-generation-drupal/

PHPMaster.com:
Convert HTML to PDF with Dompdf
Aug 15, 2013 @ 15:39:16

On PHPMaster.com there's a new tutorial from Shameer C showing you how to use Dompdf to convert HTML to PDFs. The handy library makes it (relatively) simple to automatically generate the documents without the need to know the internal PDF file structure.

PDF is a standard format originally created by Adobe for representing text and images in a fixed-layout document. It’s not uncommon for a web application to support downloading data, such as invoices or reports, in PDF format, so in this article we’ll go through how we can easily generate PDF documents using PHP.

He starts off with the installation (via Composer) and the creation of a simple "Hello World" document, showing how to "stream" it into a PDF file. He also includes some more advanced usage - loading an external HTML, drawing lines, changing fonts, adding page numbers and additional text to the page.

tagged: html pdf convert tutorial dompdf adobe

Link: http://phpmaster.com/convert-html-to-pdf-with-dompdf

PHPMaster.com:
Extract Objects from an Access Database with PHP, Part 2
Jun 03, 2013 @ 15:37:24

PHPMaster.com has posted the second part of their series covering connecting PHP to a legacy Access database. In part one they introduced you to working with the data and how to extract the object. In this new part they look at specific file types and how to pull out their data.

In this second part we’ll learn how to extract Acrobat PDF documents and take a brief look at a selection of image formats. The only similarity that PDF, GIF, PNG, etc., have when stored in an Access database is that they are all wrapped in an OLE container consisting of a variable length header and trailer. As we shall see, the trailer can be ignored as it was with the package discussed in Part 1. The header is more useful, but doesn’t contain all the information we need.

They get into the details of the headers for each type of object and include screenshots of what they look like in a hex viewer. The code for the complete class is included in the post, making it easy to drop in and get started with your transition from database object to external file.

tagged: extract object access database tutorial series pdf image

Link: http://phpmaster.com/extract-ole-objects-from-an-access-database-using-php-2

Web & PHP Magazine:
May 2013 Issue - "Time Travel"
May 09, 2013 @ 18:23:53

The Web & PHP Magazine has officially released their latest issue - the May 2013 edition, "Time Travel".

You may already be familiar with Git, but did you know that it can rewrite time itself? GitHubber Ben Straub explains all in this month's issue, which also features tutorials on extracting XML data from RSS, using Composer to manage project dependencies and using the PHP Content Repository. If you've ever thought about starting a PHP user group, Atlanta PHP User Group co-organiser Kevin Roberts shares his secrets - plus, there's our regular columns on agile, big data and testing.

Articles included in this latest issue include:

  • "Change the Past" (Ben Straub)
  • "Why do User Groups?" (Kevin Roberts)
  • "Sprint Retrospective Primer" (Steffan Surdek)
  • "Urban Legends and Error Handling" (Stefan Priebsch)
  • "Managing your project's dependencies with Composer" (Jefersson Nathan de O. Chaves)

As always, this issue is available free of charge and can be downloaded directly from their site.

tagged: webandphpmagazine issue timetravel download pdf

Link: http://webandphp.com/May2013


Trending Topics: