<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[[Solution] Custom PDF margin with unknown header&#x2F;footer heights]]></title><description><![CDATA[<p>Re: <a href="/topic/2898/pdf-margin-match-dynamic-header-footer-height">PDF margin match dynamic header/footer height</a></p>
<p>Yesterday I opened this topic above and now I've found the solution for that.</p>
<p>Basically, I added the <code>header-footer</code> from pdf-utils then I set my custom header and footer inside of each</p>
<pre><code>&lt;!-- header-footer --&gt;

    &lt;body&gt;
        {{#each $pdf.pages}}
          {{#if @index}}
            &lt;div style=&quot;page-break-before: always;&quot;&gt;&lt;/div&gt;
          {{/if}}
          &lt;main class=&quot;main&quot;&gt;
            &lt;header class=&quot;header&quot;&gt;
              {{{@root.template.header}}}
            &lt;/header&gt;

            &lt;footer class=&quot;footer&quot;&gt;
              {{{@root.template.footer}}}
            &lt;/footer&gt;
          &lt;/main&gt;
        {{/each}}
    &lt;/body&gt;
</code></pre>
<p>and on my default template I made this on <code>script</code> tag.</p>
<pre><code>&lt;!-- main-template --&gt;

&lt;script&gt;
    const header = document.getElementById('main-header');
    const footer = document.getElementById('main-footer');

    if (header &amp;&amp; footer) {
        const prevOptions = window.JSREPORT_CHROME_PDF_OPTIONS || {};

        window.JSREPORT_CHROME_PDF_OPTIONS = {
            ...prevOptions,
            marginTop: `${header.offsetHeight + 15}px`,
            marginBottom: `${footer.offsetHeight + 15}px`,
        };
    }
&lt;/script&gt;
</code></pre>
<p>also, these are my HTML and CSS</p>
<pre><code>&lt;!-- main-template --&gt;

    &lt;div id=&quot;main-header&quot; class=&quot;fixed hidden&quot;&gt;
        {{{template.header}}}
    &lt;/div&gt;
        
    &lt;div id=&quot;main-footer&quot; class=&quot;fixed hidden&quot;&gt;
         {{{template.footer}}}
    &lt;/div&gt;
</code></pre>
<pre><code>// main-template.css

.hidden {
    visibility: hidden;
}

.fixed {
    position: fixed;
}
</code></pre>
<p>The <code>template.header</code> and <code>template.footer</code> I expect to receive as data:</p>
<pre><code>// data

{
    &quot;template&quot;:{
        &quot;header&quot;:&quot;&lt;div style='width: 100%; height: 250px; background: green; text-align: center'&gt;HEADER LEGAL&lt;/div&gt;&quot;,
        &quot;footer&quot;:&quot;&lt;div style='width: 100%; height: 30px; background: red; text-align: center'&gt;FOOTER BACANA {pageNumber}/{pageTotal}&lt;/div&gt;&quot;
    }
}
</code></pre>
<hr />
<h3>Explanation</h3>
<p>The <code>window.JSREPORT_CHROME_PDF_OPTIONS</code> works in a dynamic way so if I change it, it will affect my PDF config and update it visually on the generated document.</p>
<p>The <code>main-header</code> and <code>main-footer</code> are invisible and fixed, so they are on &quot; <code>DOM</code> &quot; and then I'm able to get their heights on <code>script</code> tag and set them as <code>marginTop</code> and <code>marginBottom</code> of my PDF.</p>
<p>The reason it <strong>HAS</strong> to be on <code>script</code> is because you don't have access to either <code>window</code> or <code>document</code> on script files.</p>
<hr />
<h3>Result</h3>
<p>The result is something like this:</p>
<p><img src="/uploads/files/1692359554656-upload-80b36750-c954-434f-a0f3-11933d8cb82e.png" alt="0_1692359553196_upload-80b36750-c954-434f-a0f3-11933d8cb82e" class="img-responsive img-markdown" /></p>
<hr />
<p>If anyone needs an example, I will create one and send as a reply here.</p>
]]></description><link>https://forum.jsreport.net/topic/2899/solution-custom-pdf-margin-with-unknown-header-footer-heights</link><generator>RSS for Node</generator><lastBuildDate>Mon, 17 Aug 2026 04:43:30 GMT</lastBuildDate><atom:link href="https://forum.jsreport.net/topic/2899.rss" rel="self" type="application/rss+xml"/><pubDate>Fri, 18 Aug 2023 12:00:46 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to [Solution] Custom PDF margin with unknown header&#x2F;footer heights on Fri, 18 Aug 2023 14:24:47 GMT]]></title><description><![CDATA[<p>Re: <a href="/topic/2898/pdf-margin-match-dynamic-header-footer-height">PDF margin match dynamic header/footer height</a></p>
<p>Yesterday I opened this topic above and now I've found the solution for that.</p>
<p>Basically, I added the <code>header-footer</code> from pdf-utils then I set my custom header and footer inside of each</p>
<pre><code>&lt;!-- header-footer --&gt;

    &lt;body&gt;
        {{#each $pdf.pages}}
          {{#if @index}}
            &lt;div style=&quot;page-break-before: always;&quot;&gt;&lt;/div&gt;
          {{/if}}
          &lt;main class=&quot;main&quot;&gt;
            &lt;header class=&quot;header&quot;&gt;
              {{{@root.template.header}}}
            &lt;/header&gt;

            &lt;footer class=&quot;footer&quot;&gt;
              {{{@root.template.footer}}}
            &lt;/footer&gt;
          &lt;/main&gt;
        {{/each}}
    &lt;/body&gt;
</code></pre>
<p>and on my default template I made this on <code>script</code> tag.</p>
<pre><code>&lt;!-- main-template --&gt;

&lt;script&gt;
    const header = document.getElementById('main-header');
    const footer = document.getElementById('main-footer');

    if (header &amp;&amp; footer) {
        const prevOptions = window.JSREPORT_CHROME_PDF_OPTIONS || {};

        window.JSREPORT_CHROME_PDF_OPTIONS = {
            ...prevOptions,
            marginTop: `${header.offsetHeight + 15}px`,
            marginBottom: `${footer.offsetHeight + 15}px`,
        };
    }
&lt;/script&gt;
</code></pre>
<p>also, these are my HTML and CSS</p>
<pre><code>&lt;!-- main-template --&gt;

    &lt;div id=&quot;main-header&quot; class=&quot;fixed hidden&quot;&gt;
        {{{template.header}}}
    &lt;/div&gt;
        
    &lt;div id=&quot;main-footer&quot; class=&quot;fixed hidden&quot;&gt;
         {{{template.footer}}}
    &lt;/div&gt;
</code></pre>
<pre><code>// main-template.css

.hidden {
    visibility: hidden;
}

.fixed {
    position: fixed;
}
</code></pre>
<p>The <code>template.header</code> and <code>template.footer</code> I expect to receive as data:</p>
<pre><code>// data

{
    &quot;template&quot;:{
        &quot;header&quot;:&quot;&lt;div style='width: 100%; height: 250px; background: green; text-align: center'&gt;HEADER LEGAL&lt;/div&gt;&quot;,
        &quot;footer&quot;:&quot;&lt;div style='width: 100%; height: 30px; background: red; text-align: center'&gt;FOOTER BACANA {pageNumber}/{pageTotal}&lt;/div&gt;&quot;
    }
}
</code></pre>
<hr />
<h3>Explanation</h3>
<p>The <code>window.JSREPORT_CHROME_PDF_OPTIONS</code> works in a dynamic way so if I change it, it will affect my PDF config and update it visually on the generated document.</p>
<p>The <code>main-header</code> and <code>main-footer</code> are invisible and fixed, so they are on &quot; <code>DOM</code> &quot; and then I'm able to get their heights on <code>script</code> tag and set them as <code>marginTop</code> and <code>marginBottom</code> of my PDF.</p>
<p>The reason it <strong>HAS</strong> to be on <code>script</code> is because you don't have access to either <code>window</code> or <code>document</code> on script files.</p>
<hr />
<h3>Result</h3>
<p>The result is something like this:</p>
<p><img src="/uploads/files/1692359554656-upload-80b36750-c954-434f-a0f3-11933d8cb82e.png" alt="0_1692359553196_upload-80b36750-c954-434f-a0f3-11933d8cb82e" class="img-responsive img-markdown" /></p>
<hr />
<p>If anyone needs an example, I will create one and send as a reply here.</p>
]]></description><link>https://forum.jsreport.net/post/12539</link><guid isPermaLink="true">https://forum.jsreport.net/post/12539</guid><dc:creator><![CDATA[ed-ateixeira]]></dc:creator><pubDate>Fri, 18 Aug 2023 14:24:47 GMT</pubDate></item></channel></rss>