GithubHelp home page GithubHelp logo

reports-pdf-page-settings's Introduction

How to tune PDF page settings for HTML -> PDF reports

Basic principles of HTML to PDF formatting in YARG / CUBA reporting:

Useful resources:

Let's imagine that we want to create a report with landscape orientation, page numbers, fixed page header and footer on each page.

We will start with HTML structure:

<body>
  <h1>Clients report</h1>

  <!-- Custom HTML header -->
  <div id="header">
    <h2>Annual Report of our Company</h2>
  </div>

  <!-- Custom HTML footer -->
  <div id="footer">
    <h2>Address: William Road</h2>
    <span class="custom-footer-page-number">Number: </span>
  </div>

  <#assign clients = Root.bands.Clients />

  <#list clients as client>
  <!-- New page for each client  -->
  <div class="custom-page-start" style="page-break-before: always;">
    <h2>Client</h2>

    <p>Name: ${client.fields.title}</p>
    <p>Summary: ${client.fields.summary}</p>
  </div>
  </#list>
</body>

Here we define header and footer blocks that will be printed on each PDF page. Also we use special page-break-before: always CSS property. It will generate page break before each client info block.

As you can see we use FreeMarker statements to insert data to our template. See complete FreeMarker reference here: http://freemarker.org/docs/

We will use the following CSS code to tune our PDF page representation:

body {
  font: 12pt Georgia, "Times New Roman", Times, serif;
  line-height: 1.3;
}

@page {
  /* switch to landscape */
  size: landscape;
  /* set page margins */
  margin: 0.5cm;
  /* Default footers */
  @bottom-left {
    content: "Department of Strategy";
  }
  @bottom-right {
    content: counter(page) " of " counter(pages);
  }
}

And this CSS code to set heade/footer positions:

/* footer, header - position: fixed */
#header {
  position: fixed;
  width: 100%;
  top: 0;
  left: 0;
  right: 0;
}

#footer {
  position: fixed;
  width: 100%;
  bottom: 0;
  left: 0;
  right: 0;
}

After that we need to fix paddings of main content to prevent content and header/footer overlapping:

/* Fix overflow of headers and content */
body {
  padding-top: 50px;
}
.custom-page-start {
  margin-top: 50px;
}

What if we want to insert page number to custom position? That's it:

.custom-footer-page-number:after {
  content: counter(page);
}

Full report template and sample project are hosted on GitHub: https://github.com/cuba-labs/reports-pdf-page-settings

If you want to insert images to your PDF files, see our existing documentation: https://doc.cuba-platform.com/reporting-6.5/template_html.html See Embedded pictures section.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.