NielsSteensma / Dhalang

Generate PDFs and make screenshots of HTML using Puppeteer in Ruby
MIT License
40 stars 22 forks source link

User options pdf #16

Closed Scaffale closed 4 years ago

Scaffale commented 4 years ago

Added all options (with default) for the user to customise pdf. Options taken from https://github.com/puppeteer/puppeteer/blob/main/docs/api.md#pagepdfoptions.

Changed: lib/Dhalang/puppeteer.rb

...
        HTTP_AUTHENTICATION_CREDENTIALS = ''
        private_constant :HTTP_AUTHENTICATION_CREDENTIALS

        DEFAULT_OPTIONS = {
            scale: 1,
            displayHeaderFooter: false,
            headerTemplate: '',
            footerTemplate: '',
            printBackground: true,
            landscape: false,
            pageRanges: '',
            format: 'A4',
            width: '',
            height: '',
            margin: { top: 36, right: 36, bottom: 20, left: 36 },
            preferCSSPageSiz: false
        }
        private_constant :DEFAULT_OPTIONS
...
... (around line 73)
                    httpAuthenticationCredentials: options.has_key?(:http_authentication_credentials) ? options[:http_authentication_credentials] : HTTP_AUTHENTICATION_CREDENTIALS
                },
                pdfOptions: DEFAULT_OPTIONS.map { |option, value| [option, options.has_key?(option) ? options[option] : value] }.to_h
            }.to_json
        end
...

and

lib/js/pdf-generator.js

changed line:

await page.pdf({
            ...{ path: configuration.tempFilePath },
            ...configuration.pdfOptions
        });
NielsSteensma commented 4 years ago

Cool, thanks for contributing! I will check the PR during the weekend as i'm quite busy with work atm.

brianlow commented 4 years ago

Just tried this to set margins :+1:, thanks for the PR