Create a PDF document with a custom header and footer by converting an HTML template using itext7

I attempted to utilize the following HTML template in an effort to convert it to a PDF using iText7, however, I am facing an issue where both the header and footer are not aligning to their designated positions. You can view the example I used here. I am steering away from using the @Page header and footer properties as I am aiming to accommodate dynamic content in both sections with more than 3 rows of extended space.

Below is the iText code snippet along with the result:

ConverterProperties properties = new ConverterProperties();
//properties.setFontProvider(fontProvider);
properties.setMediaDeviceDescription(new MediaDeviceDescription(MediaType.PRINT));
PdfWriter writer = new PdfWriter("out.pdf");
PdfDocument pdf = new PdfDocument(writer);
pdf.setTagged();
PageSize pageSize = PageSize.LETTER;
pdf.setDefaultPageSize(pageSize);

OutlineHandler outlineHandler = OutlineHandler.createStandardHandler();
properties.setOutlineHandler(outlineHandler);

//html template (templateOutput)
HtmlConverter.convertToPdf(templateOutput, pdf, properties);
byte[] pdfData = byteArrayOutputStream.toByteArray();
pdf.close();

Any suggestions on how to address this issue would be greatly appreciated. Thank you.

Answer №1

Check out this HTML example that has been successfully processed by pdfHTML. The multiline header is displayed exactly as expected at the top of the page:

<!DOCTYPE html>
<html>

<head>
    <style>
        #header {
            position: running(header);
        }

        @page {
            margin-top: 100px;
            @top-center {
                content: element(header);
            }
        }
    </style>
</head>

<body>

<div id="header">Header line 1<br/>Header line 2<br/>Header line 3</div>

<p>Content of the page</p>

</body>
</html>

When rendered into a PDF, the page maintains the layout as shown below:

https://i.sstatic.net/lPlVm.png

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Can a Java command line application be executed from Python on an AWS EC2 instance?

I've been exploring machine learning for chemical modelling using Python. I'm looking to execute a Java app (via command line using Python's subprocess.call) and run a Python webserver. Can this setup be implemented on AWS EC2? Currently, I ...

What is the best way to include a div element with a dynamic animation on a webpage?

I'm attempting to create a laser beam that can shoot enemies on the screen, much like in classic games such as Space Invaders or Galaga. However, I am encountering difficulties getting the laser to move when I click the button. Below is the code I hav ...

Can you confirm the mobile type, please? Using JavaScript to display a div only once based on the mobile type

Is there a correct way to determine the type of mobile device I'm using? Are there alternative methods to check for the mobile type? Take a look at my approach in the code below. How can I test this using a tool? Does anyone have insights on checki ...

Issue with Bootstrap 5: Mobile Menu Failure to Expand

I’ve been struggling with this issue for days now. I’ve searched everywhere for a solution, but to no avail. That’s why I’m turning to the experts here for help :-) The problem I’m facing is that my mobile menu won’t open. Nothing happens when ...

Switching between numerical and alphabetical input using JQuery

How can I switch between allowing character and numeric input in a textbox? I currently have a JQuery function that restricts input to numbers only $('.numeric').on('input', function (event) { this.value = this.value.replace(/[^0 ...

Is it possible to activate the :active pseudoclass by pressing the 'enter' key on the keyboard using solely CSS?

The CSS: a:focus { opacity: .75 } a:active { transform: translateY(4px) } The purpose: When keyboard users tab to the link, the :focus style serves as a visual indicator Upon pressing the enter key to activate the link, the :active style provides ...

Tips for rearranging button placements by utilizing multiple owl carousels across various webpages

I have successfully implemented two owl carousels on my Shopify website, and they are both functioning properly. However, I am facing an issue with the navigation buttons on the second page. The navigation buttons seem to be picking up a style that I had i ...

What could be causing my body to resist adapting to the information on this website?

Is there a way to adjust content for a page using an overlay grid without exceeding the body boundaries? I'm facing this issue and struggling to find a solution. I've attempted to make a body reference in CSS and adjust the height, but no progres ...

The BorderWidth property is set to 2px, however, it is not visible on the

Struggling with adding a border to a div in my app built with react and material-ui. Here is the snippet of code I've been working with: <div className={classes.search}> ... </div> search: { ... borderWidth: '2px', borde ...

Encountering issues following the execution of setObfuscatedProfileId/setObfuscatedAccountId

As I work on my android app and set up subscription/in-app purchases, I encountered a challenge. Prior to initiating the billing flow, I make use of setObfuscatedProfileId/setObfuscatedAccountId to define a specific id for the purchase. However, this actio ...

Difficulty encountered when trying to customize a polymer element that has been expanded (paper-slider)

I've been customizing the Polymer paper-slider element to handle an enumerated list and cycle through these values in the slider instead of just showing numeric values. However, I'm struggling with getting the styles to align properly. When you r ...

Overcoming Challenges with Ajax Success in Bootstrap DataTable Modal

Recently, I created a webpage featuring a top fixed navbar and a CRUD datatable with pagination supported by buttons that trigger modals. However, I'm encountering an issue where upon clicking the delete button in the modal to remove a record, the mod ...

Angular 2: Dynamically positioning content within a div overlay

I have made some customizations to a PrimeNg TabView and placed it inside a custom component to achieve the following: https://i.sstatic.net/mjWED.gif As you can see in the image, the tabview content is set to overflow-x: hidden to enhance the appearance ...

Ways to avoid losing data when a page is refreshed

After encountering a frustrating issue with my form, I turned to research in hopes of finding a solution. However, each attempt has resulted in disappointment as the data is lost every time the page is refreshed. If anyone has advice on how to prevent th ...

Tips on how to avoid encoding certain special characters in a string when encoding. It's important to note that only the string itself needs to be encoded, not the entire URL

Original sentence "<bodytext>@#$%&</bodytext>" Desired output after encoding %3CbodyText%3E%40%23%24%25%26amp%3B%3C%2FbodyText%3E Actual result after ASCII or UTF-8 encoding %3CbodyText%3E%40%23%24%25%26%3C%2FbodyText%3E My code s ...

Utilize field variables for anonymous listener functionality

I'm facing a common issue with Android development and need help finding a solution. When dealing with multiple buttons or text views that require similar listeners attached to them, the challenge lies in having each listener access different field va ...

Having issues with implementing CSS3 animations on my webpage

I'm having trouble implementing a bouncing mouse animation on my website. The code works perfectly on another site, but on mine, it's not functioning at all. Here is the CSS code: .mouse { display: block; margin: 0 auto; text-alig ...

Switching the image by clicking on the link

Looking for assistance with a code that displays three button images and fades in the relevant div when clicked using jQuery... http://jsfiddle.net/ttj9J/11/ HTML <a class="link" href="#" data-rel="content1"><img src="http://i.imgur.com/u1SbuRE ...

Annoying animation triggered upon loading of the page using AngularJS

I'm encountering an issue with a webpage element that has a hide/show animation. Despite the initial state being hidden, when the page loads, it still displays the hide animation. I attempted to set ng-show="false", but the hide animation persists upo ...

I am experiencing an issue where my mobile responsive website consistently shows the smallest CSS width view. What could be the root of this

Currently, I am developing a mobile-responsive website specifically catered to iPhone 6 Plus and smaller models (iPhone 6 & 5 included). When I preview the site using "Inspect Element" in the Chrome browser with these device settings, everything appears t ...