This is an archived forum post. The information may be outdated. Contact us if you have any questions.

changing page orientation via html or javascript

nutrislice wrote on 2012-10-03:
We have a system that dynamically switches from a 1 page portrait calendar to a 2 page landscape calendar once there is too much text to fit on a single page. Currently we do this server-side, using a different template for each orientation and estimating whether it will be too much for one page. But this is messy because we have to estimate the number of lines based on estimated font widths and estimated line breaks in order to guess when to switch from 1 page to 2 pages, and its really tough to do accurately. With javascript it would be much easier to simply detect when the content gets too big for the single page and restructure the html a bit into two landscape pages and set a few different css classes. I'd like to switch to this method, but this brings up two questions:

1) Currently we set page orientation on the server side using the PDFCrowd python API:


Can this be set somehow in the html or via javascript, so the page orientation of the pdf will switch between portrait and landscape?

2) Will the rendering engine wait until all javascript is executed and the page's DOM is restructured before converting it to an html? In other words, I am wondering whether, if we add javascript code that changes the DOM structure from an initial portrait page to two landscape pages, which runs inline right when the page loads, whether we can be sure this will finish executing before pdfcrowd takes a snapshot of the html for a pdf. Can you provide some insight into how you determine when to actually render and how much DOM manipulation via javascript can reliably be done when the page is loaded or loading?
support wrote on 2012-10-04:
1/ It is not possible to set page orientation in html/javascript.

2/ Restructuring the page's DOM should work. A snapshot for a pdf is taken when
- There is no more javascript code to execute in the current pipeline.
- There are no pending ajax requests. If an ajax connection becomes idle then it is aborted.

So to execute your javascript code reliably:
- Execute it on the DomReady event.
- Do not use setInterval() or setTimeout().
- Avoid ajax requests that take too long to complete.
nutrislice wrote on 2012-10-05:
Thanks for the info.
I assume by DOMReady event you mean window.onload or a jquery wrapper function $(function() { ... }) . If this isn't the case, let me know. Otherwise I think it seems to be working and thanks for responsiveness.