Create PDF using HTML string

I am trying to Create a PDF using an HTML string which includes inline and internal CSS.
In html, I have image as the background of the HTML and the desired Pdf output should also have image as the background.
To achieve the output , we have change the size of the Pdf to the dimension of an image
But it generates white blank space on the right and bottom side of the pdf which is not desired.
Could you please suggest the solution to remove the white space from the pdf? I need the size of the pdf page as the size of blankcertificate.jpg.

C# Code -
HtmlLoadOptions objLoadOptions = new HtmlLoadOptions();
objLoadOptions.PageInfo.Margin = new MarginInfo(0, 0, 0, 0);
Aspose.Pdf.Document doc = new Aspose.Pdf.Document(new
MemoryStream(Encoding.UTF8.GetBytes(htmlContent.ToString())), objLoadOptions);
PdfPageEditor ppe = new PdfPageEditor(doc);
double w1 = doc.Pages[1].Rect.Width;
double h1 = doc.Pages[1].Rect.Height;
float k = (float)(w1 / h1);
ppe.Zoom = k;
ppe.PageSize = new Aspose.Pdf.PageSize((float)h1, (float)w1);
ppe.PageSize.IsLandscape = true;
string FileName = “Aspos123e_” + DateTime.Now.ToString(“dd-MM-yyyy hh-mm”) + “.pdf”;
ppe.Save(@“C:\Desktop” + FileName);

Summary

blankcertificate.jpg (84.1 KB)

HTML

<style>
body{
        background-image:url('~/blankcertificate.jpg');
        position: absolute;
        background-repeat: no-repeat;
        width:800px;
        height:618px;
}
</style>
<div id="mainDiv">
<div id="certificateBody" style="padding-left: 40px; position: absolute;">
<div id="certificateMessage" style="padding-top: 250px;">
<p style="font-size: 25px; font-weight: 450; margin-top: 0px; margin-bottom: 10px;">[PreferredName]</p>
<p style="font-size: 16px; font-weight: 450; margin-top: 0px; margin-bottom: 5px;">has successfully met all the requirements to be a</p>
<p style="font-size: 25px; font-weight: 450; margin-top: 5px; margin-bottom: 5px;">[CertificationName] Certified</p>
<p style="font-size: 16px; font-weight: 450; margin-top: 0px; margin-bottom: 5px;">[CompletionDate]</p>
<p style="font-size: 16px; font-weight: 450; margin-top: 0px; margin-bottom: 0px;"><em>Certificate Number: [CertificateNumber]</em></p>
</div>
<div id="certificateSignature" style="padding-top: 70px; margin: 0px;"><img class="signature" style="height: 50; width: 200;" src="~/Signature1.png" />
<p id="signDetails" style="font-size: 15px; font-weight: 450; margin-top: 0px; margin-bottom: 0px;"><b>ABC XYZ </b><br />ASDFGHK<br />UIOP </p>
</div>
</div>
</div>

@Pooja123

Thank you for contacting support.

Would you please share the PDF document generated using Aspose.PDF for .NET 18.4 in your environment so that we may proceed to help you out.

@Farhan.Raza Sure, I have shared the Pdf doc which is generating white spaces. I need the size of the pdf page as the size of blankcertificate.jpg. I have tried whatever there is on the forum but dint found any luck on those solution. Could you please enlighten what is the mistake. Thank you.

Summary

blankcertificate.jpg (84.1 KB)
Aspos123e_11-04-2018 05-59.pdf (302.2 KB)

@Pooja123

We have worked with the data and have been able to notice the problem of larger page instead of setting smaller values for the height and width of page. A ticket with ID PDFNET-44520 has been logged in our issue management system for further investigation and resolution. However, we have modified your code snippet to minimize the white space. It does not produce the output exactly as per your requirements but somewhat better page is generated. We have attached generated PDF file for your kind reference Test_18.4.pdf. Please try using following code snippet in your environment until the logged issue is investigated.

HtmlLoadOptions objLoadOptions = new HtmlLoadOptions(dataDir);
objLoadOptions.PageInfo.Margin = new MarginInfo(0, 0, 0, 0);
Aspose.Pdf.Document doc = new Aspose.Pdf.Document(new MemoryStream(Encoding.UTF8.GetBytes(htmlContent.ToString())), objLoadOptions);
string FileName = "Aspos123e_" + DateTime.Now.ToString("dd-MM-yyyy hh-mm") + ".pdf";
foreach (Page page in doc.Pages)
{
   page.SetPageSize(page.Rect.Width, page.Rect.Height);
}
doc.Save(dataDir + FileName);

We are sorry for the inconvenience.

@Farhan.Raza We have already achieved this style of the pdf given by you. Bu the main issue is the white space. We are trying to manipulate HTMLstring as well to get the desired output. Can u please tell me if there is any another way in Aspose.Pdf.

@Pooja123

You can trim the white space on a PDF page by using below code snippet. Generated PDF document will display the image as per your requirements.

Document document = new Document(@"D:\Files\Aspos123e_12-04-2018 12-08.pdf");

// get page to trim white space
Page pdfPage = document.Pages[1];

// get the content boundaries
Rectangle contentBBox = pdfPage.CalculateContentBBox();

// set Page CropBox and MedioBos as per content boundries to tirm white space
pdfPage.CropBox = contentBBox;
pdfPage.MediaBox = contentBBox;

// save the resultant PDF
document.Save(@"D:\Files\AfterTrim_18.4.pdf");

We have attached generated PDF file for your kind reference AfterTrim_18.4.pdf. Please feel free to contact us if you need any further assistance.

The issues you have found earlier (filed as ) have been fixed in this update. This message was posted using BugNotificationTool from Downloads module by MuzammilKhan