Skip to content

Instantly share code, notes, and snippets.

@kathoef
Created October 21, 2022 13:50
Show Gist options
  • Save kathoef/022be92a556f66c7f2fbfa4d0cf6d37c to your computer and use it in GitHub Desktop.
Save kathoef/022be92a556f66c7f2fbfa4d0cf6d37c to your computer and use it in GitHub Desktop.
LiaScript exporter empty PDF problem

Using liascript PDF exporter,

$ docker run -it -v $PWD:/things ubuntu:22.04 bash
$ apt update
$ apt install wget xz-utils python3
$ wget --quiet https://nodejs.org/download/release/v14.20.1/node-v14.20.1-linux-x64.tar.xz
$ mkdir -p /usr/local/lib/
$ tar xf node-v14.20.1-linux-x64.tar.xz -C /usr/local/lib/
$ export PATH=/usr/local/lib/node-v14.20.1-linux-x64/bin:$PATH
$ apt install --no-install-recommends --yes     libglib2.0-dev     libnss3     libnspr4     libatk1.0-dev     libatk-bridge2.0-dev     libcups2-dev     libdrm-dev     libdbus-1-dev     libatspi2.0-dev     libx11-dev     libxcomposite-dev     libxdamage-dev     libxext-dev     libxfixes-dev     libxrandr-dev     libgbm-dev     libxcb1-dev     libxkbcommon-dev     libpango1.0-dev     libcairo2-dev     libasound2-dev
$ npm install -g @liascript/exporter --unsafe-perm
$ cat project/README.md
# Title
Lorem ipsum.
$ liaex --format pdf -i project/README.md
Compiled in DEV mode. Follow the advice at https://elm-lang.org/0.19.1/optimize for better performance and smaller assets.
(node:4827) UnhandledPromiseRejectionWarning: Error: Failed to launch the browser process!
[1021/125829.490887:ERROR:zygote_host_impl_linux.cc(90)] Running as root without --no-sandbox is not supported. See https://crbug.com/638180.

TROUBLESHOOTING: https://github.com/puppeteer/puppeteer/blob/main/docs/troubleshooting.md

    at onClose (/usr/local/lib/node-v14.20.1-linux-x64/lib/node_modules/@liascript/exporter/node_modules/puppeteer/lib/cjs/puppeteer/node/BrowserRunner.js:241:20)
    at Interface.<anonymous> (/usr/local/lib/node-v14.20.1-linux-x64/lib/node_modules/@liascript/exporter/node_modules/puppeteer/lib/cjs/puppeteer/node/BrowserRunner.js:231:68)
    at Interface.emit (events.js:412:35)
    at Interface.close (readline.js:530:8)
    at Socket.onend (readline.js:254:10)
    at Socket.emit (events.js:412:35)
    at endReadableNT (internal/streams/readable.js:1333:12)
    at processTicksAndRejections (internal/process/task_queues.js:82:21)
(Use `node --trace-warnings ...` to show where the warning was created)
(node:4827) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:4827) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
$ patch < index.patch
$ liaex --format pdf -i project/README.md
Compiled in DEV mode. Follow the advice at https://elm-lang.org/0.19.1/optimize for better performance and smaller assets.
depending on the size of the course, this can take a while, please be patient...
$ npm -g ls | grep puppeteer
| +-- puppeteer@13.7.0

Testing puppeteer PDF export,

$ mkdir npmproj && cd npmproj
$ npm init
$ npm install puppeteer@13.7.0 --save --unsafe-perm
$ node pdf.js

File contents,

$ cat index.patch

--- /usr/local/lib/node-v14.20.1-linux-x64/lib/node_modules/@liascript/exporter/dist/index.js	2022-10-21 13:26:53.361741961 +0000
+++ /usr/local/lib/node-v14.20.1-linux-x64/lib/node_modules/@liascript/exporter/dist/index.js_patched	2022-10-21 13:43:30.569052137 +0000
@@ -12560,7 +12560,8 @@
             '--disable-features=IsolateOrigins',
             '--disable-site-isolation-trials',
             '--unhandled-rejections=strict',
-            '--disable-features=BlockInsecurePrivateNetworkRequests', 
+            '--disable-features=BlockInsecurePrivateNetworkRequests',
+            '--no-sandbox',
         ],
         headless: argument['pdf-preview'] ? false : true
     });

$ cat example.html

<html>
    <head>
        <title>Example</title>
    </head>
    <body>
        <p>Hello world!</p>
    </body>
</html>

$ cat pdf.js (adapted from here)

'use strict';

const puppeteer = require('puppeteer');

(async () => {
  const browser = await puppeteer.launch({
        args: [
            //'--headless',
            '--no-sandbox'
        ]
  });
  const page = await browser.newPage();
  //let url = 'https://google.com/' // works
  //let url = 'file:///home/jovyan/npmproj/example.html' // works
  // $ liaex --format web -i README.md
  //let url = 'file:///home/jovyan/npmproj/output/index.html' // does not work, empty PDF!
  // $ python3 -m http.server 54321 --directory output
  let url = 'http://localhost:54321/' // works
  await page.goto(url, {
    waitUntil: 'networkidle2',
  });
  // page.pdf() is currently supported only in headless mode.
  // @see https://bugs.chromium.org/p/chromium/issues/detail?id=753118
  await page.pdf({
    path: 'test.pdf',
    format: 'letter',
  });

  await browser.close();
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment