HTML to PDF API

Convert html, htm files to pdf with a single POST request. Upload a file as multipart/form-data and the API returns a download URL for the converted file.

Input

html, htm

Output

pdf (application/pdf)

Credit cost

2 per file

Max file size

Per plan (10–500 MB)

Endpoint

POSThttps://api.fileswitch.co/api/v1/converter/html-to-pdf

Authentication is optional for guests and recommended for production. Send either an API key or a JWT access token as a Bearer token. Authenticated requests deduct 2 credits from your balance; failed conversions are refunded. See Authentication for details.

Quick start

The same request in every supported language. Each example uploads a file and prints the downloadUrl from the response.

Example request
curl -X POST https://api.fileswitch.co/api/v1/converter/html-to-pdf \
  -H "Authorization: Bearer fc_YOUR_API_KEY" \
  -F "file=@input.html" \
  -F "pageSize=A4" \
  -F "pageWidthMm=0" \
  -F "pageHeightMm=0" \
  -F "printBackground=true" \
  -F "marginTop=20px" \
  -F "marginRight=20px" \
  -F "marginBottom=20px" \
  -F "marginLeft=20px" \
  -F "conversionDelay=0" \
  -F "waitForElementTimeout=30000" \
  -F "javascriptEnabled=true" \
  -F "viewportWidth=1366" \
  -F "viewportHeight=1024" \
  -F "cssMediaType=screen" \
  -F "scale=1"

Parameters

Send file plus any optional options. Every option is optional and falls back to its default when omitted.

FieldTypeDefaultDescription
filebinaryThe file to convert. Accepts .html , .htm
pageSizestringA4

Output page size. Overridden by pageWidthMm/pageHeightMm when set.

Values: A0, A1, A2, A3, A4, A5, A6, A7, A8, A9, B0, B1, B2, B3, B4, B5, Letter, Legal, Ledger, Tabloid

pageWidthMmnumber0

Custom page width in millimeters. 0 = use pageSize.

Range: 0 .. 30000

pageHeightMmnumber0

Custom page height in millimeters. 0 = use pageSize.

Range: 0 .. 30000

landscapebooleanfalse

Render the page in landscape orientation.

printBackgroundbooleantrue

Print the CSS background graphics.

marginTopstring20px

Top margin as a CSS length (e.g. 20px, 12mm).

marginRightstring20px

Right margin as a CSS length.

marginBottomstring20px

Bottom margin as a CSS length.

marginLeftstring20px

Left margin as a CSS length.

headerTextstring

Repeated on every page header. Use headerHtml for styled or dynamic headers.

footerTextstring

Repeated on every page footer. Use footerHtml for styled or dynamic footers.

headerHtmlstring

HTML header inserted on every page. Spans with classes pageNumber, totalPages, title, and date are auto-filled (see example below). Takes precedence over headerText.

footerHtmlstring

HTML footer inserted on every page. Spans with classes pageNumber, totalPages, title, and date are auto-filled (see example below). Takes precedence over footerText.

pageRangestring

Only convert the selected pages, e.g. "1-3,5" or "1,2,3". Omitted or empty converts every page.

conversionDelaynumber0

Delay in seconds after the page loads before rendering the PDF. Useful for slow-loading or animated content.

Range: 0 .. 30

waitForElementstring

Wait for this CSS selector to appear in the DOM before converting (e.g. "#chart-loaded").

waitForElementTimeoutnumber30000

How long to wait for waitForElement (milliseconds) before failing.

Range: 1000 .. 120000

javascriptEnabledbooleantrue

Allow web pages to run JavaScript before rendering.

userJsstring

JavaScript executed in the page before conversion begins, e.g. "document.body.style.background = '#fff'".

userCssstring

Additional CSS applied to the page before conversion.

hideElementsstring

Comma-separated CSS selectors of elements to hide during conversion (e.g. ".cookie-banner, #ads").

showElementsstring

Comma-separated CSS selectors of elements to keep visible; all other elements are hidden.

viewportWidthnumber1366

Browser viewport width in pixels.

Range: 200 .. 4000

viewportHeightnumber1024

Browser viewport height in pixels.

Range: 200 .. 4000

cssMediaTypestringscreen

Emulate a CSS media type, e.g. screen or print, before converting.

scalenumber1

Scale of the rendered content (0.1 to 2). 1 = 100%.

Range: 0.1 .. 2

loadLazyContentbooleanfalse

Scroll through the page to trigger lazy-loaded images and content before converting.

avoidBreakElementsstring

Comma-separated CSS selectors that pages should not break across.

breakBeforeElementsstring

Comma-separated CSS selectors that should start a new page before them.

breakAfterElementsstring

Comma-separated CSS selectors that should start a new page after them.

Example request bodyjson
{
  "file": "input.html",
  "pageSize": "A4",
  "pageWidthMm": 0,
  "pageHeightMm": 0,
  "landscape": false,
  "printBackground": true,
  "marginTop": "20px",
  "marginRight": "20px",
  "marginBottom": "20px",
  "marginLeft": "20px",
  "headerText": "",
  "footerText": "",
  "headerHtml": "",
  "footerHtml": "",
  "pageRange": "",
  "conversionDelay": 0,
  "waitForElement": "",
  "waitForElementTimeout": 30000,
  "javascriptEnabled": true,
  "userJs": "",
  "userCss": "",
  "hideElements": "",
  "showElements": "",
  "viewportWidth": 1366,
  "viewportHeight": 1024,
  "cssMediaType": "screen",
  "scale": 1,
  "loadLazyContent": false,
  "avoidBreakElements": "",
  "breakBeforeElements": "",
  "breakAfterElements": ""
}

Examples

Drop-in snippets for the options above.

HTML footer with page numbershtml
<span class='left'>Confidential</span>
<span class='right'>Page <span class='pageNumber'></span> of <span class='totalPages'></span></span>
HTML header with title and datehtml
<span class='left'><span class='title'></span></span>
<span class='right'><span class='date'></span></span>
Styled header with inline CSShtml
<style>
  .left { float: left; font-size: 9px; color: #666; }
  .right { float: right; font-size: 9px; color: #666; }
</style>
<span class='left'>Quarterly Report</span>
<span class='right'>Page <span class='pageNumber'></span></span>
Convert only specific pagesbash
curl -X POST https://api.fileswitch.co/api/v1/converter/html-to-pdf \
  -H "Authorization: Bearer fc_YOUR_API_KEY" \
  -F "file=@report.html" \
  -F "pageRange=1-3,5"

Response

Every conversion endpoint returns the same success / message / data envelope. The data object is a Conversion record with a temporary downloadUrl.

Response bodyjson
{
  "success": true,
  "message": "Conversion completed",
  "data": {
    "id": "b10a5f2e-8c1e-4b3a-9c1d-2e3f4a5b6c7d",
    "inputFormat": "html",
    "outputFormat": "pdf",
    "inputFileName": "document.html",
    "outputFileName": "document.pdf",
    "inputFileSize": 45210,
    "outputFileSize": 88431,
    "status": "completed",
    "error": null,
    "creditCost": 2,
    "downloadUrl": "/api/v1/converter/download/conversions%2Fb10a5f2e-8c1e-4b3a-9c1d-2e3f4a5b6c7d%2Foutput.pdf",
    "createdAt": "2026-07-31T13:00:00.000Z",
    "completedAt": "2026-07-31T13:00:02.000Z"
  }
}

Prepend https://api.fileswitch.co/api/v1 to the downloadUrl and download with the same credentials. Files are retained temporarily and auto-deleted. See the full field reference in Response Schema.

Related conversions

Ready to convert html, htmpdf? Start free — no credit card required.