Hey!
We’ve been using GroupDocs.Conversion for converting templates written in HTML into printable A4 size PDF documents.
I’ve been searching and looking around this forum, but I can’t find anything about customizing page margins for the resulting PDF, except for this thread Options for Conversion in which it’s mentioned that the named feature is not supported.
The linked thread seems to be from 2015, though, so I was wondering whether such a thing has been implemented yet, as having specific margin sizes is important for our client.
Right now, it seems there is no way to keep the document looking correct while having custom margin sizes.
Our input HTML looks more or less like this:
<html>
<head>
<style id="styles">
:root {
counter-set: pages 0;
}
@page {
counter-increment: pages;
}
@page {
size: A4;
margin-left: 20mm;
margin-right: 10mm;
@bottom-right-corner {
content: counter(page) "/" counter(pages);
font-size: 8.5pt;
}
}
body {
font-size: 10.5pt;
}
</style>
</head>
<body>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla sed egestas magna. Aenean mollis mi nec ligula condimentum, a ultricies purus volutpat. Vivamus felis est, viverra sit amet pellentesque eu, consequat non dui. Suspendisse gravida placerat est vitae luctus. Nullam ullamcorper, risus quis ultricies condimentum, diam mauris ultricies velit, vitae pellentesque arcu nisi vitae risus. Proin massa risus, aliquam nec posuere nec, commodo et odio. Ut eros sem, finibus eget congue eu, commodo id nunc. Maecenas accumsan nulla faucibus lacinia lobortis. Vivamus in malesuada est. Phasellus ullamcorper condimentum sagittis. Sed nulla mi, pharetra ac aliquet sit amet, porta id quam. Aenean ante leo, pharetra non fermentum id, semper a turpis. Curabitur sit amet arcu iaculis, hendrerit justo nec, volutpat ipsum. Etiam mollis enim a tortor semper, eu auctor lectus dapibus. Nullam sagittis, urna viverra consequat accumsan, enim metus luctus enim, ut tempor velit metus nec tellus. Ut dapibus luctus velit a venenatis.
</body>
</html>
And the code to convert from HTML to PDF:
var htmlString = "THE ABOVE HTML";
var outputPath = "path/to/output.pdf";
// ConvertStringToStream creates a MemoryStream containing the HTML
using (var converter = new Converter(() => ConvertStringToStream(htmlString)))
{
converter.Convert(outputPath, new PdfConvertOptions()
{
PageOrientation = PageOrientation.Portrait,
Format = PdfFileType.Pdf,
PageSize = PageSize.A4,
Dpi = 300,
Width = 2480,
Height = 3508,
MarginLeft = 0,
MarginRight = 0,
});
}
Is there any chance you could tell me how to achieve such margins that I’ve specified in the CSS which could be smaller than what the converter adds as default (around 3.5cm on the left and right)?
PS. We do also have a license for GroupDocs.Viewer if that might help solve this issue.
Thank you in advance!