Convert Markdown to PDF with a Simple API Call

Markdown is one of the most popular formats for writing documentation, reports, notes, blog posts, technical specifications, and README files. To turn these Markdown documents into professionally formatted PDF files, you only need two simple steps:

  1. Convert the Markdown into HTML
  2. Send the HTML to the PDFmyURL API to generate a PDF

Since most programming languages can convert Markdown to HTML in just one line of code, and the PDFmyURL API needs only a single HTTP request, the entire Markdown-to-PDF workflow is extremely compact and easy to integrate into any application or automation pipeline.

Table of Contents


Why Markdown must be converted to HTML

Markdown is a lightweight text format intended for writing—not for layout. It does not define fonts, margins, page size, page breaks, or advanced formatting. PDF renderers require HTML and CSS, which provide full layout control.

Therefore, the correct workflow is:

Markdown → HTML → PDF

This is the same approach used by GitHub, GitLab, MkDocs, Pandoc, documentation generators, and most modern publishing tools.

Fortunately, converting Markdown to HTML is very easy. Every major programming language has a Markdown library that transforms your text into HTML instantly.

Once you have the HTML, you simply send it to the PDFmyURL API using the html= parameter, and the API returns the final PDF.


Basic Markdown → HTML → PDF workflow

No matter which language you use, the process looks like this:

1. html = convertMarkdownToHtml(markdown)
2. POST html to https://pdfmyurl.com/api
3. Save the returned PDF bytes

Below we show minimal, ready-to-use examples in several languages.


Convert Markdown to PDF using curl

If you already have an HTML version of your Markdown (for example from a file or command-line tool), you can post it directly with curl:

curl --data-urlencode "html=

Hello World

" \ "https://pdfmyurl.com/api?license=yourlicensekey" \ -o markdown.pdf

This is useful if you convert Markdown to HTML using another program such as:

  • Pandoc
  • marked (Node.js)
  • markdown-cli
  • Python's markdown module

Python example

Python has excellent Markdown libraries. This example converts Markdown to HTML and sends it to the PDFmyURL API in just a few lines.

import markdown
import requests

md = "# My Report\nThis is **Markdown**."
html = markdown.markdown(md)

pdf = requests.post(
    "https://pdfmyurl.com/api?license=yourlicensekey",
    data={"html": html}
).content

open("report.pdf", "wb").write(pdf)

Node.js example

Node.js developers can use the marked library to transform Markdown into HTML. This is perfect for server-side rendering, microservices, or static site generators.

// install first:
//   npm install node-fetch marked

const fs = require('fs');
const fetch = require('node-fetch');
const { marked } = require('marked'); // marked v5+ exports like this
const { URLSearchParams } = require('url');

const md = '# Title\nSome *Markdown* content.';
const html = marked(md);

(async () => {
  const params = new URLSearchParams();
  params.append('html', html);

  const res = await fetch('https://pdfmyurl.com/api?license=yourlicensekey', {
    method: 'POST',
    body: params
  });

  if (!res.ok) {
    throw new Error('API error: ' + res.status + ' ' + res.statusText);
  }

  const pdf = await res.buffer();
  fs.writeFileSync('report.pdf', pdf);
  console.log('Saved report.pdf');
})();

PHP example

In PHP you can use any Markdown library (e.g. erusev/parsedown). Once converted, the HTML is posted to the PDFmyURL API.

<?php
require 'Parsedown.php';
$Parsedown = new Parsedown();

$md = "# Hello\nThis is **Markdown**.";
$html = $Parsedown->text($md);

$pdf = file_get_contents(
    "https://pdfmyurl.com/api?license=yourlicensekey",
    false,
    stream_context_create([
        'http' => [
            'method'  => 'POST',
            'header'  => "Content-Type: application/x-www-form-urlencoded",
            'content' => http_build_query(['html' => $html])
        ]
    ])
);

file_put_contents("report.pdf", $pdf);

Ruby example

Ruby’s syntax makes Markdown conversion and PDF generation very compact.

require "redcarpet"
require "net/http"

md = "# Heading\nSome text."
html = Redcarpet::Markdown.new(Redcarpet::Render::HTML).render(md)

uri = URI("https://pdfmyurl.com/api?license=yourlicensekey")
pdf = Net::HTTP.post_form(uri, { "html" => html }).body

File.binwrite("report.pdf", pdf)

Java example

Java developers can use popular Markdown libraries such as flexmark-java.

// Convert Markdown to HTML (example using flexmark)
Parser parser = Parser.builder().build();
Node document = parser.parse(md);
HtmlRenderer renderer = HtmlRenderer.builder().build();
String html = renderer.render(document);

// Send HTML to PDFmyURL
String api = "https://pdfmyurl.com/api?license=yourlicensekey";

HttpURLConnection conn = (HttpURLConnection) new URL(api).openConnection();
conn.setDoOutput(true);
conn.setRequestMethod("POST");

String data = "html=" + URLEncoder.encode(html, "UTF-8");
conn.getOutputStream().write(data.getBytes(StandardCharsets.UTF_8));

try (InputStream in = conn.getInputStream();
     FileOutputStream out = new FileOutputStream("report.pdf")) {
    in.transferTo(out);
}

Styling Markdown with CSS

One of the advantages of converting Markdown to HTML before generating the PDF is that you can apply any CSS you like. This allows you to create reports or documents that look consistent across your entire application.

Example: Add CSS to style headings and paragraphs

--data-urlencode "css=h1{color:#444;font-family:Arial;} p{line-height:1.4;}"

You can use:

  • Your own styles, by saving default CSS in our members area
  • A GitHub-style Markdown theme
  • Syntax highlighting themes for code blocks
  • Branding and layout templates

Popular use cases for Markdown → PDF

Markdown-to-PDF conversion is widely used in:

  • Technical documentation
  • Automated PDF reporting
  • Exporting README files
  • Static site publishing
  • Invoice and receipt generators
  • Developer tools
  • Internal knowledge bases
  • CMS and blog platforms

With PDFmyURL, these pipelines become extremely simple to implement.


Summary

To convert Markdown to PDF, first transform the Markdown into HTML using a library in your preferred programming language. Then send the HTML to PDFmyURL using the html parameter. Since both steps are lightweight and require very little code, you can integrate Markdown-to-PDF generation into any system within minutes.

See our main HTML to PDF API documentation for all available options.