Pagina 1 di 1

Conversione .md in pdf con pandoc + CSS

Inviato: sab 25 mar 2023, 17:11
da joe
Volevo convertire in PDF un documento scritto in testo semplice, simil-markdown (poi da aggiustare a seconda del risultato). Allo stesso tempo volevo provare a rendere il risultato un po' più carino del default aggiungendo un CSS e quindi passando per l'HTML.

Ho preso spunto da qui:

https://stackoverflow.com/questions/238 ... wn-css-pdf

Codice: Seleziona tutto

# Install pandoc and dependencies
sudo apt update
sudo apt install pandoc
sudo apt install wkhtmltopdf  # a dependency to convert HTML To pdf

# Download the github.css CSS style theme
wget https://raw.githubusercontent.com/simov/markdown-viewer/master/themes/github.css

# Convert test.md to test.pdf using the github.css CSS style theme
pandoc -f gfm -t html5 --metadata pagetitle="test.md" --css github.css \
test.md -o test.pdf
Io pandoc e wkhtmltopdf li avevo già installati.
Però a quanto pare pandoc vuole utilizzare un javascript per la conversione da html a pdf, mi dava errore e si lamentava dell'assenza di questo qui:

https://github.com/pubpub/pagedjs-cli

Allora l'ho installato, però creando un pacchetto slackware con:

Codice: Seleziona tutto

npm2tgz pagedjs-cli
Alla fine l'errore non c'è più ma ne salta fuori un altro:

Codice: Seleziona tutto

$ pagedjs-cli template.html -o test.pdf
◴ Loading: template.htmlError: Could not find Chromium (rev. 1108766). This can occur if either
 1. you did not perform an installation before running the script (e.g. `npm install`) or
 2. your cache path is incorrectly configured (which is: /home/user/.cache/puppeteer).
For (2), check out our guide on configuring puppeteer at https://pptr.dev/guides/configuration.
    at ChromeLauncher.resolveExecutablePath (file:///usr/lib64/node_modules/pagedjs-cli/node_modules/puppeteer-core/lib/esm/puppeteer/node/ProductLauncher.js:101:27)
    at ChromeLauncher.executablePath (file:///usr/lib64/node_modules/pagedjs-cli/node_modules/puppeteer-core/lib/esm/puppeteer/node/ChromeLauncher.js:178:25)
    at ChromeLauncher.launch (file:///usr/lib64/node_modules/pagedjs-cli/node_modules/puppeteer-core/lib/esm/puppeteer/node/ChromeLauncher.js:64:37)
    at async Printer.setup (file:///usr/lib64/node_modules/pagedjs-cli/src/printer.js:65:19)
    at async Printer.render (file:///usr/lib64/node_modules/pagedjs-cli/src/printer.js:78:4)
    at async Printer.pdf (file:///usr/lib64/node_modules/pagedjs-cli/src/printer.js:267:14)
    at async file:///usr/lib64/node_modules/pagedjs-cli/src/cli.js:165:10
Anche con pandoc l'errore è lo stesso:

Codice: Seleziona tutto

$ pandoc -f gfm -t html5 --metadata pagetitle="test.md" --css github.css test.md -o test.pdf
◴ Loading: ./html2pdf24192-0.htmlError: Could not find Chromium (rev. 1108766). This can occur if either
 1. you did not perform an installation before running the script (e.g. `npm install`) or
 2. your cache path is incorrectly configured (which is: /home/user/.cache/puppeteer).
For (2), check out our guide on configuring puppeteer at https://pptr.dev/guides/configuration.
    at ChromeLauncher.resolveExecutablePath (file:///usr/lib64/node_modules/pagedjs-cli/node_modules/puppeteer-core/lib/esm/puppeteer/node/ProductLauncher.js:101:27)
    at ChromeLauncher.executablePath (file:///usr/lib64/node_modules/pagedjs-cli/node_modules/puppeteer-core/lib/esm/puppeteer/node/ChromeLauncher.js:178:25)
    at ChromeLauncher.launch (file:///usr/lib64/node_modules/pagedjs-cli/node_modules/puppeteer-core/lib/esm/puppeteer/node/ChromeLauncher.js:64:37)
    at async Printer.setup (file:///usr/lib64/node_modules/pagedjs-cli/src/printer.js:65:19)
    at async Printer.render (file:///usr/lib64/node_modules/pagedjs-cli/src/printer.js:78:4)
    at async Printer.pdf (file:///usr/lib64/node_modules/pagedjs-cli/src/printer.js:267:14)
    at async file:///usr/lib64/node_modules/pagedjs-cli/src/cli.js:165:10
Error producing PDF.
Credevo dipendesse dall'assenza di Chromium ma pur aggiungendo il pacchetto da AlienBob, niente da fare. L'errore resta.
Insomma per fare una cosa abbastanza banale è saltata fuori una complicazione assurda.

Avete per caso qualche idea su come risolvere?

Re: Conversione .md in pdf con pandoc + CSS

Inviato: sab 25 mar 2023, 17:47
da erio
pandoc slackbuild prima di essere compilato vuole una marea di pacchetti haskell,puo dipendere forse da questo

Re: Conversione .md in pdf con pandoc + CSS

Inviato: sab 25 mar 2023, 21:46
da joe
Non credo dipenda da quello, piuttosto direi che il problema ha a che fare con quel componente pagedjs-cli.
Infatti se ho ben capito la conversione da HTML a PDF può essere fatta scegliendo diversi "motori" e quello sopra sarebbe solo uno dei tanti. Non so perché pandoc lo scelga di default, potrebbe dipendere anche dal CSS di github? Non ne ho idea...

Ad ogni modo due prove veloci:
  1. Provo a specificare il motore convertitore con l'opzione --pdf-engine, e ne scelgo uno differente "wkhtmltopdf"

    Codice: Seleziona tutto

    pandoc -f gfm -t html5 --metadata pagetitle="test.md" --pdf-engine wkhtmltopdf --css github.css test.md -o test.pdf
  2. Provo a lasciar perdere pandoc e a richiamare direttamente pagedjs-cli per convertire un file HTML in PDF, lasciando quindi un attimo da parte il markdown:

    Codice: Seleziona tutto

    $ pagedjs-cli template.html -o test.pdf
    ◴ Loading: template.htmlError: Could not find Chromium (rev. 1108766). This can occur if either
     1. you did not perform an installation before running the script (e.g. `npm install`) or
     2. your cache path is incorrectly configured (which is: /home/user/.cache/puppeteer).
    For (2), check out our guide on configuring puppeteer at https://pptr.dev/guides/configuration.
        at ChromeLauncher.resolveExecutablePath (file:///usr/lib64/node_modules/pagedjs-cli/node_modules/puppeteer-core/lib/esm/puppeteer/node/ProductLauncher.js:101:27)
        at ChromeLauncher.executablePath (file:///usr/lib64/node_modules/pagedjs-cli/node_modules/puppeteer-core/lib/esm/puppeteer/node/ChromeLauncher.js:178:25)
        at ChromeLauncher.launch (file:///usr/lib64/node_modules/pagedjs-cli/node_modules/puppeteer-core/lib/esm/puppeteer/node/ChromeLauncher.js:64:37)
        at async Printer.setup (file:///usr/lib64/node_modules/pagedjs-cli/src/printer.js:65:19)
        at async Printer.render (file:///usr/lib64/node_modules/pagedjs-cli/src/printer.js:78:4)
        at async Printer.pdf (file:///usr/lib64/node_modules/pagedjs-cli/src/printer.js:267:14)
        at async file:///usr/lib64/node_modules/pagedjs-cli/src/cli.js:165:10
    
Conclusione:
il problema è proprio quel componete lì: pagedjs-cli
Che non funziona per qualche santo come dovrebbe.

Per il resto pandoc, funziona anche utilizzando latex per la conversione non ricordo se si appoggi a pdflatex graze al "motore" html2pdf, ma qualcosa del genere.