├── .gitignore ├── composer.json ├── index.php ├── readme.md └── serverless.yml /.gitignore: -------------------------------------------------------------------------------- 1 | /composer.lock 2 | /.idea/ 3 | /.serverless 4 | /vendor 5 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "require": { 3 | "ext-json": "*", 4 | "bref/bref": "^1.2", 5 | "knplabs/knp-snappy": "^1.2" 6 | }, 7 | "scripts": { 8 | "deploy": [ 9 | "composer install --optimize-autoloader --no-dev", 10 | "serverless deploy" 11 | ] 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /index.php: -------------------------------------------------------------------------------- 1 | 'utf-8', 14 | 'page-size' => 'A4', 15 | 'margin-bottom' => 0, 16 | 'margin-left' => 0, 17 | 'margin-top' => 0, 18 | 'margin-right' => 0, 19 | 'disable-smart-shrinking' => true, 20 | 'disable-javascript' => true, 21 | ]; 22 | 23 | if (isset($event['options'])) { 24 | $options = \array_merge( 25 | $options, 26 | \json_decode($event['options'], true) 27 | ); 28 | } 29 | 30 | $output = $pdf->getOutputFromHtml($event['html'], $options); 31 | 32 | if (empty($output)) { 33 | throw new \RuntimeException('Unable to generate the html'); 34 | } 35 | 36 | return \base64_encode($output); 37 | }; 38 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | ## wkhtmltopdf-lambda-php 2 | 3 | Converting HTML to PDF on AWS Lambda. 4 | 5 | Using: 6 | 7 | - [Bref](https://bref.sh/) + [Serverless Framework](https://serverless.com/) to deploy the function; 8 | - [wkhtmltopdf](https://wkhtmltopdf.org/) binary as a layer. If you want to compile your own layer: [Compiling wkhtmltopdf for use inside an AWS Lambda](https://tech.mybuilder.com/compiling-wkhtmltopdf-aws-lambda-with-bref-easier-than-you-think/) 9 | 10 | **Input:** 11 | 12 | ````json 13 | { 14 | "html": "..." 15 | } 16 | ```` 17 | 18 | **Output:** 19 | 20 | Base64 PDF. 21 | 22 | ```json 23 | JVBERi0xLjQKMSAwIG9iago8 ... 24 | ``` 25 | 26 | **Suggestion of pre-compiled layers:** 27 | 28 | https://github.com/brandonlim-hs/wkhtmltopdf-aws-lambda-layer 29 | -------------------------------------------------------------------------------- /serverless.yml: -------------------------------------------------------------------------------- 1 | service: wkhtmltopdf 2 | 3 | provider: 4 | name: aws 5 | region: sa-east-1 6 | runtime: provided.al2 7 | stage: prod 8 | memorySize: 4096 # Change to your needs 9 | lambdaHashingVersion: 20201221 10 | timeout: 29 11 | 12 | plugins: 13 | - ./vendor/bref/bref 14 | 15 | functions: 16 | html-to-base64-pdf: 17 | handler: index.php 18 | environment: 19 | FONTCONFIG_PATH: /opt/etc/fonts 20 | description: 'HTML to Base64 PDF' 21 | layers: 22 | - ${bref:layer.php-80} 23 | - 'arn:aws:lambda:sa-east-1:347599033421:layer:wkhtmltopdf-0_12_6:1' 24 | - 'arn:aws:lambda:sa-east-1:347599033421:layer:amazon_linux_fonts:1' 25 | --------------------------------------------------------------------------------