Forum How do I...?

Define page size with querystring?

misc
I'm working on a document that contains a list of items. The list may vary in length from day to day, but I want the page height to change to accommodate the entire list regardless of how long it is.

My current thinking is to have the number of items as a querystring on the document's URL (?items=123) and use JS to read this and dynamically set the height of the @page accordingly. Is that possible, or is the page size set before the JavaScript is run?
mikeday
This is possible, JavaScript is run before layout so if it creates a <style> element with @page then it will be applied. Please keep in mind that if the list gets very long then creating an absurdly tall page may upset some PDF viewers! There might be value in allowing page breaks given that PDF is a paginated format. :)
misc
I agree with that, but I've been set a requirement of a single page. The list should only vary between approx 180 - 220 so it shouldn't cause a problem of extreme page sizes.

I'm using the following JS, but I'm getting "TypeError: value is not an object". Am I missing something obvious?

<script type="text/javascript">
    function addStyleString(str) {
        var node = document.createElement('style');
        node.innerHTML = str;
        document.body.appendChild(node);
    }

    var params = (new URL(document.location)).searchParams;
    var items = params.get("items");
    var itemsHeight = 5 * (Math.ceil (items / 3));
    var pageHeight = 150 + itemsHeight;
    addStyleString('@page { height: ' +  pageHeight + 'mm;}');
</script>
mikeday
Prince does not support the URL() constructor, but that should give a ReferenceError.