Page size apparently can now be "almost" accurately controlled without using the debug interface.
The following is a method of creating a PDF with nearly the exact dimensions of its contents using headless chrome.
<head>
<style>
html, body {
width: fit-content;
height: fit-content;
margin: 0px;
padding: 0px;
}
</style>
<style id=page_style>
@page { size: 100px 100px ; margin : 0px }
</style>
</head>
This prepares for making the pdf to fit the page, but will not be right, since the page size has been set to an arbitrary value of 100x100.
After the document has been rendered, the following is used to set the page size correctly at the bottom of the page:
<script>
window.onload(fixpage);
function fixpage() {
renderBlock = document.getElementsByTagName("html")[0];
renderBlockInfo = window.getComputedStyle(renderBlock)
// fix chrome page bug
fixHeight = parseInt(renderBlockInfo.height) + 1 + "px"
pageCss = `@page { size: \${renderBlockInfo.width} \${fixHeight} ; margin:0;}`
document.getElementById("page_style").innerHTML = pageCss
}
</script>
This approach eliminates the header/footer and deals with a numerical issue with pixel conversion to pdf.
One more thing
Chrome currently has a bug with the calculation of absolute height of a div when you use the CSS
line-height: normal;
This will make the page calculation too short and lead to an extra pdf page being generated. You can fix this using:
line-height: unset;
Throughout your CSS. You won't get an accurate height without it!