Welcome Guest Search | Active Topics | Sign In | Register

Page Size Problem Options
Alex
Posted: Thursday, November 14, 2013 12:38:49 PM
Rank: Newbie
Groups: Member

Joined: 11/13/2013
Posts: 5
Hi,

I have a create pdf function in a web app that sends multiple html pages (that may or may not have different sizes) via ajax to be rendered into a single multi-page pdf. Everything is working flawlessly except that instead of each page being sized according to PageSize and OutputArea, all pages are 8.5 * 11. Below is a code snippet where the html pages are passed to ConvertHtml and rendered. I have checked the option values in debug mode and they are correct.

Please let me know what I need to change to fix this issue.

Cheers,

Alex


Code: C#
if (Convert.ToInt32(context.Request.Form["filecount"]) < 1) return;
                    var pdfDoc = new PdfDocument();
                    var fileCount = Convert.ToInt32(context.Request.Form["filecount"]);
                    for (var i = 0; i < fileCount; i++)
                    {
                        var hpfPrint = context.Request.Files["file" + i];
                        if (hpfPrint != null && hpfPrint.ContentLength > 0)
                        {
                            var pdfPage = pdfDoc.Pages.Add();
                            pageWidth = float.Parse(context.Request.Form["pagewidth" + i]);
                            pageHeight = float.Parse(context.Request.Form["pageheight" + i]);
                            topMargin = float.Parse(context.Request.Form["topmargin" + i]);
                            bottomMargin = float.Parse(context.Request.Form["bottommargin" + i]);
                            leftMargin = float.Parse(context.Request.Form["leftmargin" + i]);
                            rightMargin = float.Parse(context.Request.Form["rightmargin" + i]);
                            outputAreaWidth = pageWidth - (leftMargin + rightMargin);
                            outputAreaHeight = pageHeight - (topMargin + bottomMargin);
                            var options = new HtmlToPdfOptions
                            {
                                PageSize = new SizeF(pageWidth, pageHeight),
                                OutputArea = new RectangleF(leftMargin, topMargin, outputAreaWidth, outputAreaHeight),
                                AutoFitX = HtmlToPdfAutoFitMode.None,
                                AutoFitY = HtmlToPdfAutoFitMode.None
                            };
                            var htmlStr = new StreamReader(hpfPrint.InputStream).ReadToEnd();
                            HtmlToPdf.ConvertHtml(htmlStr, pdfPage, options);
                        }
                    }
                    if (File.Exists(pdfFileName))
                    {
                        File.Delete(pdfFileName);
                    }
                    pdfDoc.Save(pdfFileName);



eo_support
Posted: Thursday, November 14, 2013 1:05:40 PM
Rank: Administration
Groups: Administration

Joined: 5/27/2007
Posts: 24,196
Hi,

Do not call PdfDoc.Pages.Add(). When you call pdfDoc.Pages.Add() it will always add the page at default size. However if you call:

HtmlToPdf.ConvertHtml(htmlStr, pdfDoc, options);

Then the converter will automatically add pages using the page size you set in your options object. Note the second argument is pdfDoc, not pdfPage.

Thanks!
Alex
Posted: Thursday, November 14, 2013 2:46:09 PM
Rank: Newbie
Groups: Member

Joined: 11/13/2013
Posts: 5
Hi,

Thanks. I knew it had to be something simple. The page sizes are now spot on.

I am having one last problem (which probably also has a simple answer). I am getting a blank page after each converted page (both have same page size). So, my 21 page sample file makes a 42 page pdf.

How do I make it not create a blank page after each page?

Cheers,

Alex



eo_support
Posted: Thursday, November 14, 2013 2:53:14 PM
Rank: Administration
Groups: Administration

Joined: 5/27/2007
Posts: 24,196
Hi,

It should not create a new page unless your contents can not fit on the previous page. So please check whether you have some invisible HTML elements in your HTML. For example, a DIV with a white background color can create a new page but there will be nothing visible on that page. As a simple test, you can use simple hard coded HTML string and see if it creates blank pages. If it doesn't, then you can try different HTML strings and see what triggered the problem. Alternatively, you can also try to adjust the bottom margin and see if that fixes the problem ---- sometimes it's just a little bit whitespace at the bottom of the page that gets pushed to the next page.

Thanks!

Alex
Posted: Thursday, November 14, 2013 2:57:18 PM
Rank: Newbie
Groups: Member

Joined: 11/13/2013
Posts: 5
Hi,

That sounds easy enough. Thanks again.

Cheers,

Alex
Droddy
Posted: Tuesday, May 6, 2014 1:37:56 PM
Rank: Member
Groups: Member

Joined: 1/22/2014
Posts: 20
So, if there is enough whitespace after an element at the end of the HTML page that would cause there to be an additional, blank PDF page, there is no was to tell HtmlToPdf to suppress this?
eo_support
Posted: Wednesday, May 7, 2014 7:23:15 PM
Rank: Administration
Groups: Administration

Joined: 5/27/2007
Posts: 24,196
Hi,

We automatically delete small blank pages. For example, if you have in your original HTML you have a small DIV in your page like this:

Code: HTML/ASPX
<div style="height:5px;page-break-before:always;page-break-after:always;">
</div>


Then this DIV will cause a blank page during the initial conversion stage. However the contents on this page is only 5 pixels high. In that case this page will be deleted during the "post processing" stage.

If the contents on the page is of significant height (more than 10 pixels), then we will keep them. So for example, if your HTML is like this:

Code: HTML/ASPX
<p>
    page 1
</p> 
<div style="height:100px;page-break-before:always;">
</div>
<p style="page-break-before:always;">
    page 3
</p>


Then in this case the result will be 3 pages. The second page will be blank. However because the content on that page is of significant height (in this case 100 pixels), then we will keep that page.

The reason for this is we want to be faithful to the original HTML. If there is a significant space in the original HTML, then there will be significant space in the PDF ---- the space in the PDF can be bigger due to paging, but can not be smaller. If we were delete the second page, then that would give user the impression that "page 3" is directly followed by "page 1", which does not match the original HTML.

Thanks!


You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.