3

I've just installed grails rendering plugin and would like to use it for generating PDF files. I've created simple template, but it is exported without any css styles. If I simply render template from grails, then page appears with all styles in my web browser.

So, my question is - how to correctly include CSS file during PDF generation process?

My template:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
    <link rel="stylesheet" href="${resource(dir:'css',file:'main.css')}" />
    <link rel="stylesheet" href="${resource(dir:'css',file:'webui.css')}" />
    <r:layoutResources/>
    <title>Report</title>
</head>
<body>
    <div id="content">
        <div id="center-container">
        <h1><g:message code="default.list.label" args="[entityName]" /></h1>
        <table>
            <thead>
                <tr>
                    <th class="trip">trip</th>
                </tr>
            </thead>
            <tbody>
                <tr class="odd">
                    <td>
                        ${tip}
                    </td>
                </tr>
            </tbody>
        </table>
</div>
    </div>
</body>
</html>

And I have style .odd in my webui.css, but it is not applied on the row.

Any help would be appreciated.

Edit1: I found out that styles are fetched, if I do it in the following way:

<link rel="stylesheet" href="my_appname${resource(dir:'css',file:'main.css')}" />

But I don't want to hardcode application name (this is also a base context path). Is there a better way to generate proper link to a css file?

Alex K.
  • 3,294
  • 4
  • 29
  • 41

3 Answers3

3

It may be late with my answer but I wanted to share my experience here with rendering pdf in grails. I followed the below steps and failed over to the next until I get a pdf:

  1. Used the resource plugin in the template gsp to grab a module where css was bundled.

For example: Test.gsp

<html>
<head>
  <r:require modules="bootstapApp"/>
  <r:layoutResources/>
</head>
<body>
   ....
   <r:layoutResources/>
</body>
</html>

The above worked fine but the styles were not used in the pdf after rendering. I had to fall over to step 2.

2.Started using tag as mentioned above in the problem statement. Result: No change. I wasn't able to get the styles in the pdf. Failed over to step 3

3.Added the styles inline in the template gsp. And then I was able to apply them to the pdf. Point to note here is that if you follow step 3 and you have css like bootstrap.css then inlining them in the template will be cumbersome. Even if we add them, do not forget to put them inside the media tag. For me the below worked perfectly fine:

<style type="text/css">
@media all {
    //CSS styles goes here
}
</style>
dmahapatro
  • 49,365
  • 7
  • 88
  • 117
2

Try setting grails.serverURL in Config.groovy to the app url (ex. grails.serverURL=http://localhost:8080/appname). The plugin resolves all relative links via this setting

aldrin
  • 4,482
  • 1
  • 33
  • 50
  • Thank you for the answer. It is actually set like that: development { grails.logging.jul.usebridge = true grails.serverURL = "http://localhost:8080/${appName}" } – Alex K. May 29 '12 at 11:06
  • is there an 'http' before 'localhost' ? also you can force resource to use absolute path resource(dir:'css',file:'main.css', absolute:true) to make sure the plugin gets the css file correctly. – aldrin May 29 '12 at 13:52
  • Yes, there is a 'http' before 'localhost'. Also I've tried to use absolute path, but in this case it is generated like http:// localhost:8080/appname/css/main.css which is wrong, since css lies in appname/static/css/main.css – Alex K. May 29 '12 at 14:09
  • i've observed that http://localhost:8080/appname/css/main.css redirects will anyway redirect to ../static/.. so that should not be a problem. – aldrin May 29 '12 at 14:19
  • i can't see what the issue might be. i just attempted a test app with the plugin and it worked well (using renderPdf(template: "/report", filename: 'test.pdf')) – aldrin May 29 '12 at 14:22
  • I see, seems like some local configuration issue. Thanks a lot, I will try to find it out then. – Alex K. May 29 '12 at 15:58
0

I shared my answer elsewhere, but I ended up just embedding the external file contents into the gsp for pdf rendering:

https://stackoverflow.com/a/32767378/1599616

Community
  • 1
  • 1
Scott Langeberg
  • 189
  • 1
  • 7