|
Rank: Member Groups: Member
Joined: 2/21/2012 Posts: 12
|
We have a client who would like two options for their PDFs - normal paged output, and output that is one continuous page (e.g. a single page with the page size set so that it can include all content).
We are using HtmlToPdf in an ASP.NET site (still evaluating actually).
One thought would be to render the content, figure out the number of pages, then re-render with a page size set to #pages x page height. Is there a better way to do this?
|
|
Rank: Administration Groups: Administration
Joined: 5/27/2007 Posts: 24,195
|
Hi, It's probably easier for you to set a huge page height first, then render the page and use this property to get the content height: http://www.essentialobjects.com/doc/4/eo.pdf.htmltopdfresult.lastposition.aspxThis will be more accurate than #pages x page height because sometimes there are extra spaces between pages ---- for example, if a line of text cannot fit on the current page, then it will be moved to the next page, thus leaving some empty space (less than one line of text) at the end of the previous page. Such empty space does not exist in a continuous page. Thanks!
|
|
Rank: Member Groups: Member
Joined: 2/21/2012 Posts: 12
|
Here is how I implemented this. It seems inefficient to render the page twice. Can I just resize the PDFDocument instead?
Code: C#
HtmlToPdfOptions pdfOptions = new HtmlToPdfOptions();
SetPDFOptions(pdfOptions, 1000f); // Set options to a 1000 inch long page (method not shown)
// Create the PDF and find the length
PdfDocument doc = new PdfDocument();
HtmlToPdfResult res = HtmlToPdf.ConvertHtml(html, doc, pdfOptions); // html is a string with my html
float docLength = res.LastPosition + 1.25f; // Add extra length for margins
// Reset the print area and re-render
SetPDFOptions(pdfOptions, docLength);
HtmlToPdf.ConvertHtml(html, Response.OutputStream, pdfOptions);
|
|
Rank: Administration Groups: Administration
Joined: 5/27/2007 Posts: 24,195
|
Hi, You can try to call this method to move the contents from the top of the page to the bottom of the page: http://www.essentialobjects.com/doc/4/eo.pdf.pdfpage.transform.aspxThen use this property to resize the page: http://www.essentialobjects.com/doc/4/eo.pdf.pdfpage.size.aspxNote you MUST call Transform to move the contents to the bottom of the page. This is because PDF page Y coordination goes from bottom to top, so when you reduce the page height, you are actually cutting off the top portion of the page instead of the bottom portion of the page. Thanks!
|
|
Rank: Member Groups: Member
Joined: 2/21/2012 Posts: 12
|
I finally got this to work after discovering a limitation: if you set the page size > 200, the page will not render (it will just be blank). So, the new code is below:
Code: C#
float longPageLength = 200f; // if you exceed 200, the PDF does not render
SetPDFOptions(pdfOptions, longPageLength);
// Create the PDF and get length
PdfDocument doc = new PdfDocument();
HtmlToPdfResult res = HtmlToPdf.ConvertHtml(html, doc, pdfOptions);
float docLength = res.LastPosition + 1.25f; // Add extra length for margins
// Move to bottom of page and cut off top
PdfMatrix matrix = new PdfMatrix();
matrix.Translate(0, (-longPageLength + docLength)*72);
doc.Pages[0].Transform(matrix);
doc.Pages[0].Size = new PdfSize(8.5f, docLength);
// Send to user
doc.Save(Response.OutputStream);
However, when I use this technique, I lose my footer, so I will likely revert to the original (slower) method.
|
|
Rank: Administration Groups: Administration
Joined: 5/27/2007 Posts: 24,195
|
Hi,
We were not aware of the 200 inch limitation. We will look into and see what we can find.
You can either call HtmlToPdf.ConvertHtml for a second time just to render the footer, or use the ACM interface to render footer. Both methods allow you to render the footer onto an existing page. That will be faster than running HTML to PDF converter on the main contents twice.
Thanks!
|
|
Rank: Member Groups: Member
Joined: 2/21/2012 Posts: 12
|
Can you give a brief example of how I might use the methods you describe above? It will save me a bunch of time in the docs.
As for the 200 inch limitation, it is 200 exactly. If you set to 201, it just renders blank but it does report the LastPosition correctly.
Thanks for the reply. I just purchased your product because of the support I've received in this forum.
|
|
Rank: Administration Groups: Administration
Joined: 5/27/2007 Posts: 24,195
|
Hi, Thank you very much for your business. To render footer with HTML to PDF, you would do something like this:
Code: C#
//Create the PDF document
PdfDocument doc = new Document();
//Render your main contents
......
//Render footer
HtmlToPdf.Options.StartPosition = y_position_of_the_footer;
HtmlToPdf.ConvertHtml("your_footer_html", doc.Pages[0]);
Note the key is you would pass the PdfPage object directly to ConvertHtml for the footer conversion. Another way to do this is to use the ACM (PDF Creator) interface. ACM interface is a different interface that compliments the HTML to PDF interface. It's more code, much less powerful but much faster. It will take a little bit more than a few lines of code to use ACM interface. So you will want to take a look of the documentation first. You can also take a look of the source code in the sample project for All Demos -> HTML to PDF -> Advanced -> Using with PDF Creator demo to get an idea how it works. The idea is the same that you would be generating additional contents to an existing page that already has the main HTML to PDF result. Hope this helps. Please feel free to let us know if you have any more questions. We will let you know once we find out the root cause of the 200 inch issue. Thanks!
|
|
Rank: Administration Groups: Administration
Joined: 5/27/2007 Posts: 24,195
|
Hi,
We have not been able to reproduce the 200 inch issue. Can you isolate the problem into a small test project and then post the complete test code?
Thanks!
|
|
Rank: Member Groups: Member
Joined: 2/21/2012 Posts: 12
|
I have a sample project that illustrates the issue with rendering a long page using this method. How can I upload it?
|
|
Rank: Administration Groups: Administration
Joined: 5/27/2007 Posts: 24,195
|
Hi,
We have looked into your sample and replied your email. The PDF converter is working correctly. Please see the email we sent to you for more details.
Thanks!
|
|
Rank: Member Groups: Member
Joined: 2/21/2012 Posts: 12
|
I do not think you understand the problem. The PDF converter is not working correctly when the page size exceeds 200 inches. Please see my latest email for further detail and example code.
|
|
Rank: Administration Groups: Administration
Joined: 5/27/2007 Posts: 24,195
|
Hi,
We understood your problem perfectly fine but you did not understand our last reply. The root of the problem is an error in your code. Specifically you used HtmlToPdfResult.LastPosition to measure the total output height. That is wrong because HtmlToPdfREsult.LastPosition points to the last position on the last output page. HtmlToPdfResult.LastPosition is only the same as the total output height if the output is a on a single page. We have already explained this to you in our last email.
This issue is now closed.
Thanks!
|
|