Search code examples
javapdfgrailsgroovypdf-generation

Attach watermark image in each page of PDF


In my grails project I am using grails rendering plug-in to convert a GSP into PDF. PDF have 3 to 5 pages.

Now I need to attach a watermark as bottom right of each page of the PDF.

I have no idea how can I achieve this. Any help highly appreciated.

EDIT..............

My gsp which is used to generate pdf is a template.

I am using grails 2.2.0 and plugins.rendering=0.4.3.

Below is my code..,.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<style type="text/css">
    @page {
        size: 210mm 297mm;
        @bottom-right {content: element(footer)}
    }
</style>
<table border="0" align="center" cellpadding="2" cellspacing="2">
...
</table>

Solution

  • Using the rendering plugin you have access to flying saucer and its support für @page rules.

    With this you could define running elements as defined in the CSS 3 specification.

    So you could define a footer element within your gsp:

    <body>
        ...
        <div id="footer">Your Watermark</div>
        ...
    </body>
    

    And style it like this:

    <style type="text/css">
        @page {
            size: 210mm 297mm;
            @bottom-right {content: element(footer)};
            ...
        }
    
        #footer{
            position: running(footer);
        }
    
        ...
    </style>