SelectPdf Html to Pdf make Image and page in equal size

1.7k Views Asked by At

I generate a card with html and then I convert it to pdf with SelectPdf converter. The problem is, I can't seem to fit the card and pdf page perfectly, without no "white paper". My goal is that the card and pdf page are the same size, so i can print it.

Card on pdf page example

the code (c#):

HtmlToPdf converter = new HtmlToPdf();
var doc = new PdfDocument();
converter.Options.PdfPageSize = PdfPageSize.Custom;
converter.Options.PdfPageCustomSize = new SizeF(Convert.ToInt32(paperWidth), Convert.ToInt32(paperHeight));
doc = converter.ConvertHtmlString(cardString);
return doc.Save();
1

There are 1 best solutions below

0
On

If you use div element, in CSS you can use @media print to specify formatting when printed.

<html>
<style>
@media print {
        body{
            width: 21cm;
            height: 29.7cm;
       } 
    }
    body {
        margin: 0;
        padding: 0;
    }

    .pageA4 {
        width: 210mm;
        height: 297mm;
    }
</style>
<html>
<head>
</head>
<body>
    <div class="pageA4" style="background-color:yellow"><h1>page 1</h1></div>
   
</body>
</html>

If you want to know more , read this