简体   繁体   中英

Grails rendering plugin css issue

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?

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>

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM