Using Document() and PageInfo()

Need a little help trying to generate a PDF with a normal page size. When the PDF is created, it is really wide (like the equivalent of about 6-8 pages wide). I’ve tried forcing the page size by setting the PageInfo properties passed into HtmlLoadOptions, but it only appears to be effecting the height. I’d like to just force it to 8.5" x 11" with a landscape flag, but I’m not sure how to do it.


Here’s how I’m building out the Document() object:

EDIT: Updated code snippets…

public string ConvertHtmlToPdf(string htmlString, string fileName, bool isLandscape = false, bool overwriteExisting = true)
{
// Rename the file to a numbered copy if needed
if (!overwriteExisting)
fileName = GetUnusedFileName(fileName);

// Get the full saved file path with name
var fullPath = GetFullFilePath(fileName);

// New up a document object
var doc = GetBaseDocument(htmlString);

var pdfSaveOptions = new PdfSaveOptions {WarningHandler = new HandleAsposeDocumentWarnings()};

// Save the PDF file
doc.Save(fullPath, pdfSaveOptions);

return fileName;
}

private Document GetBaseDocument(string html, bool isLandscape = false)
{
var htmlLoadOptions = new HtmlLoadOptions(_fileBasePath)
{
WarningHandler = new HandleAsposeDocumentWarnings()
};

// Convert string to stream to pass into the Document ctor
var stream = new MemoryStream(Encoding.UTF8.GetBytes(html));
var doc = new Document(stream, htmlLoadOptions)
{
OptimizeSize = true,
PageInfo = GetBasePageInfo(isLandscape)
};

return doc;
}

private PageInfo GetBasePageInfo(bool isLandscape)
{
return new PageInfo
{
IsLandscape = isLandscape,
Margin = new Aspose.Pdf.MarginInfo
{
Top = 40,
Bottom = 40,
Left = 40,
Right = 40
}
};
}

I should also note that I’m using bootstrap.css as my stylesheet. Are there some tricks to getting the styles to play nice with the PDF converter engine in order to get regular page sizes?

Also, is there a debug flag I can set somewhere so it will dump out errors into the PDF it creates? There are times when it doesn’t display content at all, and messing with the HTML objects and classes can change that. I haven’t figured out what’s causing some elements to disappear some times.

Does anyone have success using bootstrap.css as the style sheet… what to use, what not to use? I can get things like

to work some times, but other times they don’t render at all in the PDF.

Figured out I needed to pass PdfSaveOptions when doing doc.Save() to get it to set the page size properly.


Now I’m fighting CSS to get elements to show…

Apparently I’m just having a conversation with myself here, but I’m posting so others can see the process I’m going through (hopefully it will help someone else).


Anyway…

I can’t get the margins to change by setting Document.PageInfo.Margin. What’s the proper way to set page margins when creating a PDF with the method in the original post??

Kizmar:
Figured out I needed to pass PdfSaveOptions when doing doc.Save() to get it to set the page size properly.
Hi Nicholas,

Thanks for contacting support and sorry for the delayed response.

In order to set the page dimensions, please follow the instructions specified over Update Page Dimensions