San Diego: PDFGenerationAPI No Longer Respects CSS Paged Media

John104
Tera Contributor

I noticed that the changes to the PDFGenerationAPI between Rome and San Diego seem to include ignoring any CSS Paged Media that you pass to the convertToPDF method.

For example, if you use the following template:

<style>
  * {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
  }

  img,
  picture,
  canvas,
  svg {
    display: block;
    max-width: 100%;
  }

  p,
  h1,
  h2,
  h3,
  h4,
  h5,
  h6 {
    overflow-wrap: break-word;
    margin-bottom: 1em;
  }

  @page {
    size: letter portrait;
    margin: 1in;

    @top-left {
      content: "Page - "counter(page);
    }
  }

  @page :first {
    margin-top: 1.75in;

    @top-left {
      content: "";
    }

    @top-center {
      content: element(runningPageHeader);
    }
  }

  #pageHeader {
    height: 1.16in;
    position: running(runningPageHeader);
  }

  #pageHeader img {
    height: 1.14in;
  }
</style>
<div id="pageHeader"><img src="https://dev113776.service-now.com/throw-me-a-frickin-bone-dr-evil-meme.png" /></div>
<p>Dr. Evil&#39;s image should appear in the header.</p>
<p>Page margins should be 1&#34;.</p>
<p> </p>
<p>The</p>
<p>Dr.</p>
<p>Evil</p>
<p>image</p>
<p>should</p>
<p>only</p>
<p>appear</p>
<p>on</p>
<p>the</p>
<p>first</p>
<p>page</p>
<p>because</p>
<p>of</p>
<p>the</p>
<p>CSS</p>
<p>Paged</p>
<p>Media</p>
<p>Query</p>
<p>that</p>
<p>allows</p>
<p>you</p>
<p>to</p>
<p>differentiate</p>
<p>the</p>
<p>first</p>
<p>page</p>
<p>&#64;page {<br /> size: letter portrait;<br /> margin: 1in;<br /><br /> &#64;top-left {<br /> content: &#34;Page -
  &#34; counter(page);<br /> }<br />}</p>
<p>&#64;page :first {<br /> margin-top: 1.75in;<br /> <br /> &#64;top-left {<br /> content: &#34;&#34;;<br /> }<br />
  &#64;top-center {<br /> content: element(runningPageHeader);<br /> }<br />}</p>

Then you run convertToPDF you will notice that the generated PDFs look different. In Rome, the Paged Media styles are enforced providing the pages margins and custom headers. However, in San Diego the Paged Media styles are ignored and the margins revert to the default and headers are not shown.

This seems like a pretty glaring bug. Has anyone else experienced this?

1 ACCEPTED SOLUTION

Jeffrey Siegel
Mega Sage

I have a fix for this From ServiceNow's support team:

 

This is a current issue in San Diego, but it is fixed in the Tokyo release. In the meantime, below is the work around.

It is possible to enable the older Code for: sn_pdfgeneratorutils.PDFGenerationAPI(); 

by setting the system property:  com.snc.pdfgenerator.html2pdf.api.version to value 1. (For San Diego the default setting is "2").

 

 

This fixed it for me!!

View solution in original post

5 REPLIES 5

Fredrik2
Tera Contributor

We are experiencing the exact same thing. After using several hours, we're not closer to find any workaround. This is creating enormous problems for us.

aasch
Kilo Sage

Hi,

same here. convertToPDFWithHeaderFooter does no longer honor any @page rule since San Diego for a customer.
Before that it worked fine.

convertToPDFWithHeaderFooter does seem to honor the page properties, which means you can still add a header and footer that way. But this of course heavily limits layout and design options.

There's a post on StackOverflow (Adding Headers, Footers, and Page Numbers to HTML Page Converted to PDF iText 7) which is about a similar problem.
One answer suggest this: "The @page part of your CSS will only be looked at by iText if you define a new MediaDeviceDescription(MediaType.PRINT)!"

According to San Diego release notes the iText5 to iText7 upgrade might still be in the works. Perhaps convertToPDFWithHeaderFooter and the other methods are missing the internal flag that the StackOverflow answer points out.


From the release notes:

iText5 PDF generator

[com.snc.pdf_generator]

Planned for deprecation in the T release.Tool to generate PDF documents.This will be replaced by iText7 PDF generator.

I'm not sure though if that is the exact same thing sn_pdfgeneratorutils uses.

Greetings,
Aaron

P.S.:

Also the suggestion to wrap the @page-rule within an "@media print {}" query did not help in our case.

Im also experiencing this issue, i hope a solution is provided asap.