PHPackages                             viavario/pdfgenerator - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [PDF &amp; Document Generation](/categories/documents)
4. /
5. viavario/pdfgenerator

ActiveLibrary[PDF &amp; Document Generation](/categories/documents)

viavario/pdfgenerator
=====================

PDF Generator using headless Chrome on AWS Lambda

1.1(7mo ago)2371GPL-3.0-onlyPHP

Since May 10Pushed 7mo ago1 watchersCompare

[ Source](https://github.com/viavario/pdfgenerator)[ Packagist](https://packagist.org/packages/viavario/pdfgenerator)[ RSS](/packages/viavario-pdfgenerator/feed)WikiDiscussions main Synced 1w ago

READMEChangelog (10)Dependencies (2)Versions (13)Used By (0)

PDFGenerator
============

[](#pdfgenerator)

PDFGenerator allows you to easily generate PDF documents or PNG screenshots of any webpage using PHP.

In the past we have used PhantomJS, wkhtmltopdf and TCPDF to generate PDF's from a given URL or HTML. However, these libraries are no longer actively developed or maintained and lack support for newer features in HTML and CSS. Google Chrome has a built in feature to take a screenshot of a webpage as PDF or PNG format with better support for new features.

Using [Amazon Lambda](https://aws.amazon.com/lambda/) and [Amazon API Gateway](https://aws.amazon.com/api-gateway/) you can minimize running costs, have scalable performance and flexible security controls. Lambda is used to run a headless Chrome as an on-demand service to take screenshots from any website or submitted HTML content without having to set up a server. You can configure security controls on the API Gateway like API keys, rate limiting, and quota to protect your screenshot service against third parties or inappropriate use.

[Puppeteer](https://pptr.dev/) is used to communicate with Chrome to load content and take screenshots programmatically.

Requirements
------------

[](#requirements)

To deploy Chrome on Amazon Lambda you will need:

1. An [Amazon AWS account](https://aws.amazon.com) to deploy the Lambda function
2. [Node.js](https://nodejs.org/en/download/) installed on your local machine
3. [Serverless](https://www.serverless.com/framework/docs/providers/aws/guide/installation/) installed on your local machine

To use this library in PHP:

1. [Composer](https://getcomposer.org/)
2. PHP 7+

---

Deploying Chrome on Amazon Lambda
---------------------------------

[](#deploying-chrome-on-amazon-lambda)

1. Go to the official [Node.js website](https://nodejs.org/en/download/), download and follow the [installation instructions](https://nodejs.org/en/download/) to install Node.js on your local machine.
2. Install the Serverless Framework via [npm](https://npmjs.org/) which was already installed when you installed Node.js.
    Open up a terminal and run the following command to install Serverless:

```
npm install -g serverless

```

Next, you'll have to [set up your credentials](https://www.serverless.com/framework/docs/providers/aws/guide/credentials/) so that Serverless is able to connect to your Amazon AWS account to deploy the Lambda function and API Gateway. The easiest way is to create a new IAM user and attach a custom JSON policy to limit the Serverless Framework's access to your AWS account. This way you don't have to sign up for an account on  either.
To limit the Serverless Framework’s access your AWS account, follow these steps to **create an IAM User** and attach a custom JSON file policy to your new IAM User. This IAM User will have its own set of AWS Access Keys.

1. Login to your AWS Account and go to the Identity &amp; Access Management (IAM) page.
2. Click on Users and then **Add user**. Enter a name in the first field to remind you this User is related to the Service you are deploying with the Serverless Framework, like `serverless-servicename-agent1`. Enable **Programmatic access** by clicking the checkbox. Click **Next** to go through to the Permissions page. Click on **Create policy**. Select the **JSON** tab, and add a JSON file. You can use [this gist](https://gist.github.com/ServerlessBot/7618156b8671840a539f405dea2704c8) as a guide.
3. When you are finished, select **Review policy**. You can assign this policy a **Name** and **Description**, then choose **Create Policy**. Check to make sure everything looks good and click **Create user**. Later, you can create different IAM Users for different apps and different stages of those apps. That is, if you don't use separate AWS accounts for stages/apps, which is most common.
4. Configure your AWS credentials by running `serverless` and selecting `Skip Adding An App`.
5. Navigate to the directory `screenshot-service` in this repository in your terminal window and install the required packages by running:

```
npm install

```

6. Open `screenshot-service/serverless.yml` and change the region on line 10 to the region you wish to deploy to on Amazon AWS. See  for more information about the available regions.

```
  region: eu-west-3

```

7. Next, deploy the screenshot service to Amazon AWS:

```
serverless deploy --stage production

```

8. In the output of the command about you can find the endpoints for the GET and POST request.
    [![Output of serverless deploy](./docs/img/serverless-output.jpg)](./docs/img/serverless-output.jpg)

If you haven't configured any API keys yet in the API Gateway for the screenshot service, you can simply copy the endpoint URL of the GET request, append `?filename=screenshot.png&url=[URL of your website]` to the endpoint URL and open it in your browser.

**Note that you have to set *API Key Required* to *false* for the */capture - GET -Method Request* in the API to test this without API Keys:**

1. Go to *Amazon API Gateway*
2. Open the API for the screenshot service
3. Click on *GET* under */capture* in the *Resources* panel
4. Click on *Method Request*
5. Click on the pencil icon next to **API Key Required**, change the setting from *true* to *false*, and click the check mark icon to save the setting.
6. Click on the *Actions* button, and choose *Deploy API* from the menu to redeploy the API to test the GET-request without API Keys.

Make sure to set **API Key Required** to *true* after you have verified that the screenshot service works in order to prevent unauthorized use or access.

For example: `https://##########.execute-api.eu-west-3.amazonaws.com/dev/capture?filename=screenshot.png&url=https://www.google.com/finance/quote/TSLA:NASDAQ?hl=en` returns a screenshot of the Tesla stock on Google Finance:

[![Example screenshot](./docs/img/example-screenshot.jpg)](./docs/img/example-screenshot.jpg)

Securing your screenshot service
--------------------------------

[](#securing-your-screenshot-service)

To protect the screenshot service against unauthorized use or access you can set up API Keys for the API Gateway. **If you do not set up API Keys for the screenshot service other people may be able to use your installation to create screenshots which will add to your bill. It is important that you secure your API Gateway.**

**Make sure to redeploy the API for the API Key requirement to take effect!**

See  on how to set up API Keys.

Installation of this library
----------------------------

[](#installation-of-this-library)

```
composer require viavario\pdfgenerator

```

Usage
-----

[](#usage)

```
