Quantcast
Channel: Aspose.Pdf Product Family
Viewing all articles
Browse latest Browse all 3131

Using Document() and PageInfo()

$
0
0
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.

Viewing all articles
Browse latest Browse all 3131

Trending Articles