Convert Markdown Documents to PDF Using md-to-pdf

Convert Markdown Documents to PDF Using md-to-pdf

Azure DevOps now has an option to link your Wiki to a repo. With this change I decided to start creating all my documentation in Markdown and then to supply my users with PDF output for things such as user guide. This allows me to source control my documents and still supply users with clean looking outputs. Here is how i went about doing this.

Create Documents Folder/Package

I started by creating a new folder for my documents and running the npm init command.

Create Folder and Run init

Install md-to-pdf from NPM

Install the md-to-pdf npm package by running npm i --save-dev md-to-pdf

Install md-to-pdf

Create Index.js file

Create a index.js file in the root directory of your folder and copy the following code.

const mdToPdf = require('md-to-pdf');
const config = {
"pdf_options": {
  "format": "Letter",
  "margin": "15mm",
  "displayHeaderFooter": true,
  "headerTemplate": "<style>section {padding-left: 10mm; padding-right: 10mm; font-family: system-ui; font-size: 11px; }</style><section><span class='title'></span> - <span class='date'></span></section>",
  "footerTemplate": "<section><div>Page <span class='pageNumber'></span> of <span class='totalPages'></span></div></section>"},
  "stylesheet_encoding": "utf-8"
  };
 const docs = ['README'];
 const start = async () => {
    for (let docName of docs) {
        config.dest = docName + '.pdf';
        const pdf = await mdToPdf(docName + '.md', config).catch(console.error);
        if (pdf) {
            console.log(pdf.filename);
        }
    }
    console.log('Done');
  }

start();

Update the docs array to include the file names of the markdown files you want to convert. In my example I’m just doing a single README file.

Generate Documents

To generate the pdf documents open a console window and run npm run test. The output for the PDFs will have the same file name as the Markdown files and will be in the same directory.

Generate Files

Comments

  1. The team was excellent at giving constructive feedback and lending their expertise to the project. addiction specialist bethesda

    ReplyDelete
  2. Nicely done, Thank you for sharing such a useful article. I had a great time. This article was fantastic to read. continue to write about
    Data Engineering Solutions 
    Advanced Data Analytics Solutions
    Business Intelligence Solutions
    Artificial Intelligence Solutions

    ReplyDelete
  3. Extremely useful information which you have shared here. This is a great way to enhance knowledge for us, and also helpful for us. Thankful to you for sharing an article like pdf editor tool to minimize pdf size online.

    ReplyDelete

Post a Comment

Popular posts from this blog

Add User As Local Administrator On Domain Controller

Horizontally Scroll A Large Power Apps Gallery

Custom Grid/Subgrid Using Office-UI-Fabric DetailsList