simonhaenisch / md-to-pdf

Hackable CLI tool for converting Markdown files to PDF using Node.js and headless Chrome.
https://www.npmjs.com/md-to-pdf
MIT License
1.12k stars 108 forks source link

feature: Add Dockerfile for easier usage and integration #293

Open PeterDaveHello opened 2 months ago

PeterDaveHello commented 2 months ago

Problem:

The md-to-pdf project currently lacks a Dockerfile, which may lead to:

  1. Complicated setup process for users who need to manually install Node.js, headless Chrome, and other dependencies on their local machines.
  2. Inconsistent runtime environments across different systems, increasing the chances of encountering issues due to varying configurations.
  3. Difficulty in deploying the tool on various platforms and cloud services that support Docker containers.
  4. Challenges in integrating md-to-pdf into CI/CD pipelines for automated conversion of Markdown files to PDF.

Solution:

Add a Dockerfile to the project, providing an official, containerized environment with all necessary dependencies pre-installed. This would simplify the setup process, ensure consistency across different systems, enhance portability, and enable seamless integration with CI/CD pipelines.

If you believe this feature would be valuable to the project, I would be happy to submit a pull request containing a basic Dockerfile. Please let me know your thoughts on this suggestion. Thank you for considering this proposal!

simonhaenisch commented 2 months ago

Hey sure why not, can we base it on the one from https://pptr.dev/guides/docker?

FROM ghcr.io/puppeteer/puppeteer:latest

RUN npm install --global md-to-pdf

CMD [md-to-pdf]

sth like this?

PeterDaveHello commented 2 months ago

We can, just the image seems to be a little bit huge, but maybe nobody cares? I don't know. Let me know if you have preference, maybe I can help test and send a PR for it.

simonhaenisch commented 2 months ago
google-chrome-stable fonts-ipafont-gothic fonts-wqy-zenhei fonts-thai-tlwg fonts-khmeros fonts-kacst fonts-freefont-ttf libxss1 dbus dbus-x11

this is the list of packages it installs (source), what would you omit? Or would you not base it on node:20? Not sure how to make it smaller 🤷🏻‍♂️

Edit: maybe by not installing Chrome, and letting the puppeteer package download it post-install instead? Hm or maybe it's better to just also add ENV PUPPETEER_SKIP_CHROMIUM_DOWNLOAD 1.

simonhaenisch commented 2 months ago
FROM ghcr.io/puppeteer/puppeteer:latest

ENV PUPPETEER_SKIP_CHROMIUM_DOWNLOAD 1

USER root

RUN npm install --global md-to-pdf

USER pptruser

ENTRYPOINT ["md-to-pdf"]

CMD ["--help"]

this is what I came up with now and seems to work fine but happy to get a better suggestion (:

$ docker build . -t md-to-pdf

then

$ docker run -v $(pwd):$(pwd) -w $(pwd) md-to-pdf readme.md

[11:06:17] generating PDF from readme.md [started]
[11:06:24] generating PDF from readme.md [completed]
PeterDaveHello commented 2 months ago

I used to use Alpine-based node.js image and install the dependencies as mentioned here, but no guarantee which is better:

https://pptr.dev/troubleshooting/#running-on-alpine

simonhaenisch commented 2 months ago

Might be better cause the Puppeteer image comes with Puppeteer pre-installed which we don't actually need. But I like that this way we don't need to define/maintain a list of deps to install, leaving that to the Puppeteer team 🤓

PeterDaveHello commented 2 months ago

Sure, sounds great, I found the image even correctly handles Chinese fonts already.