# frozen_string_literal: true require 'grover/version' require 'grover/utils' require 'active_support_ext/object/deep_dup' unless defined?(ActiveSupport) require 'grover/html_preprocessor' require 'grover/middleware' require 'grover/configuration' require 'grover/options_builder' require 'nokogiri' require 'schmooze' require 'yaml' # # Grover interface for converting HTML to PDF # class Grover # # Processor helper class for calling out to Puppeteer NodeJS library # class Processor < Schmooze::Base dependencies puppeteer: 'puppeteer' def self.launch_params ENV['GROVER_NO_SANDBOX'] == 'true' ? "{args: ['--no-sandbox', '--disable-setuid-sandbox']}" : '{args: []}' end def self.convert_function(convert_action) <<~FUNCTION async (url_or_html, options) => { let browser; try { let launchParams = #{launch_params}; // Configure puppeteer debugging options const debug = options.debug; delete options.debug; if (typeof debug === 'object' && !!debug) { if (debug.headless != undefined) { launchParams.headless = debug.headless; } if (debug.devtools != undefined) { launchParams.devtools = debug.devtools; } } // Configure additional launch arguments const args = options.launchArgs; delete options.launchArgs; if (Array.isArray(args)) { launchParams.args = launchParams.args.concat(args); } // Set executable path if given const executablePath = options.executablePath; delete options.executablePath; if (executablePath) { launchParams.executablePath = executablePath; } // Launch the browser and create a page browser = await puppeteer.launch(launchParams); const page = await browser.newPage(); // Basic auth const username = options.username; delete options.username const password = options.password; delete options.password if (username != undefined && password != undefined) { await page.authenticate({ username, password }); } // Setting cookies const cookies = options.cookies; delete options.cookies if (Array.isArray(cookies)) { await page.setCookie(...cookies); } // Set caching flag (if provided) const cache = options.cache; delete options.cache; if (cache != undefined) { await page.setCacheEnabled(cache); } // Setup timeout option (if provided) let request_options = {}; const timeout = options.timeout; delete options.timeout; if (timeout != undefined) { request_options.timeout = timeout; } // Setup viewport options (if provided) const viewport = options.viewport; delete options.viewport; if (viewport != undefined) { await page.setViewport(viewport); } const waitUntil = options.waitUntil; delete options.waitUntil; if (url_or_html.match(/^http/i)) { // Request is for a URL, so request it request_options.waitUntil = waitUntil || 'networkidle2'; await page.goto(url_or_html, request_options); } else { // Request is some HTML content. Use request interception to assign the body request_options.waitUntil = waitUntil || 'networkidle0'; await page.setRequestInterception(true); page.once('request', request => { request.respond({ body: url_or_html }); // Reset the request interception // (we only want to intercept the first request - ie our HTML) page.on('request', request => request.continue()); }); const displayUrl = options.displayUrl; delete options.displayUrl; await page.goto(displayUrl || 'http://example.com', request_options); } // If specified, emulate the media type const emulateMedia = options.emulateMedia; delete options.emulateMedia; if (emulateMedia != undefined) { if (typeof page.emulateMediaType == 'function') { await page.emulateMediaType(emulateMedia); } else { await page.emulateMedia(emulateMedia); } } // If specified, evaluate script on the page const executeScript = options.executeScript; delete options.executeScript; if (executeScript != undefined) { await page.evaluate(executeScript); } // If we're running puppeteer in headless mode, return the converted PDF if (debug == undefined || (typeof debug === 'object' && (debug.headless == undefined || debug.headless))) { return await page.#{convert_action}(options); } } finally { if (browser) { await browser.close(); } } } FUNCTION end method :convert_pdf, convert_function('pdf') method :convert_screenshot, convert_function('screenshot') end private_constant :Processor DEFAULT_HEADER_TEMPLATE = "
" DEFAULT_FOOTER_TEMPLATE = <<~HTML