puppeteer-pdf-wrapper

    0.7.0 • Public • Published

    Puppeteer PDF Wrapper

    Install

    $ [sudo] npm install [-g] puppeteer-pdf-wrapper
    

    CLI Usage

    $ puppeteer-pdf-wrapper <command> [options]
    
    Ie:
    $ puppeteer-pdf-wrapper generate -u https://google.com/
    
    -h, --help         Show available options
    -v, --version      Show version
    
    generate           Generate a PDF file
    -u, --url          The URL of the page you wish to convert to a PDF
    -l, --local-file   The file path of the local HTML page you wish to convert to a PDF
    -o, --pdf-options  Base64 encoded JSON object of Puppeteer page.pdf() options
    -t, --http-options Base64 encoded JSON object of the HTTP request options
    -n, --filename     The filename for the generated PDF
    -p, --filepath     The folder path to save the generated PDF
    -d, --dump         Dump results to stdout as base64 encoded JSON object, will not save to disk
    

    Note: Use -l or -u, not both, if both are defined, -l wins. Note: If -n/-p are used with -d, -d wins.

    <?php
    // $ [sudo] npm install -g puppeteer-pdf-wrapper
    $url = 'https://google.com';
    
    $pdfOptions = array(
    	'format' => 'Letter',
    	'printBackground' => true,
    	'margin' => array(
    		'top' => '.5in',
    		'bottom' => '.5in'
    	)
    );
    
    $httpOptions = array(
    	'cookies' => array(array(
    		'name' => 'some cookie',
    		'value' => 'some value',
    		'domain' => 'google.com',
    		'path' => '/'
    	))
    );
    
    $cmd = implode(' ', array(
    	'puppeteer-pdf-wrapper',
    	'generate -u "'.$url.'"',
    	'-o "'.base64_encode(json_encode($pdfOptions)).'"',
    	'-t "'.base64_encode(json_encode($httpOptions)).'"',
    	'-n "google.com.pdf"',
    	'-p "'.__DIR__.'"'
    ));
    
    exec($cmd);
    
    $pdfFilepath = __DIR__.'/google.com.pdf';
    $htmlFilepath = __DIR__.'/google.com.html';
    
    $results = array(
    	'pdf' => file_get_contents($pdfFilepath),
    	'html' => file_get_contents($htmlFilepath)
    );
    
    unlink($pdfFilepath);
    unlink($htmlFilepath);
    
    header('content-type: application/pdf');
    header('content-length: '.strlen($results['pdf']));
    header('content-disposition: attachment; filename="google.com.pdf"');
    
    echo $results['pdf'];
    
    ?>
    <?php
    // $ [sudo] npm install -g puppeteer-pdf-wrapper
    $url = 'https://google.com';
    
    $pdfOptions = array(
    	'format' => 'Letter',
    	'printBackground' => true,
    	'margin' => array(
    		'top' => '.5in',
    		'bottom' => '.5in'
    	)
    );
    
    $httpOptions = array(
    	'cookies' => array(array(
    		'name' => 'some cookie',
    		'value' => 'some value',
    		'domain' => 'google.com',
    		'path' => '/'
    	))
    );
    
    $cmd = implode(' ', array(
    	'puppeteer-pdf-wrapper',
    	'generate -u "'.$url.'"',
    	'-o "'.base64_encode(json_encode($pdfOptions)).'"',
    	'-t "'.base64_encode(json_encode($httpOptions)).'"',
    	'-d'
    ));
    
    // This method doesn't seem to always work - the output isn't always completely consumed by PHP - unsure why
    $results = shell_exec($cmd);
    
    $json = json_decode($results, true);
    
    $pdf = base64_decode($json['pdf']);
    
    header('content-type: application/pdf');
    header('content-length: '.strlen($pdf));
    header('content-disposition: attachment; filename="google.com.pdf"');
    
    echo $pdf;
    
    ?>

    JS Usage

    'use strict';
    
    const fs = require('fs').promises;
    const path = require('path');
    const puppeteerPdfWrapper = require('puppeteer-pdf-wrapper');
    
    return puppeteerPdfWrapper('https://google.com/', {
    	// All options can be found here: https://github.com/GoogleChrome/puppeteer/blob/master/docs/api.md#pagepdfoptions
    	format: 'Letter',
    	printBackground: true
    }, {
    	// Set a custom user agent (via: https://github.com/GoogleChrome/puppeteer/blob/master/docs/api.md#pagesetuseragentuseragent)
    	userAgent: 'some-custom-user-agent',
    	// Set any number cookies (via: https://github.com/GoogleChrome/puppeteer/blob/master/docs/api.md#pagesetcookiecookies)
    	cookies: [{
    		name: 'some-cookie',
    		value: 'some-value',
    		domain: 'google.com',
    		path: '/'
    	}]
    }).then((results) => {
    	// Returns an object with two properties 'pdf' and 'html'.
    	// results.pdf is a Buffer of the PDF document
    	// results.html is a Buffer of the HTML that was used to generate the PDF document
    
    	return Promise.all([
    		fs.writeFile(path.join(process.cwd(), 'GeneratedPDF.pdf'), results.pdf),
    		fs.writeFile(path.join(process.cwd(), 'GeneratedPDF.html'), results.html)
    	]);
    }).catch((err) => {
    	console.error(err);
    });

    Keywords

    none

    Install

    npm i puppeteer-pdf-wrapper

    DownloadsWeekly Downloads

    2

    Version

    0.7.0

    License

    Apache-2.0

    Unpacked Size

    11.9 kB

    Total Files

    5

    Last publish

    Collaborators

    • tflanagan