Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
760 views
in Technique[技术] by (71.8m points)

java - 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...