grails renderpdf插件,它是如何工作的?

时间:2012-06-15 09:09:00

标签: pdf grails button render connect

我正在尝试使用renderpdf grails插件渲染PDF, 但他们的文件很短。

我在我的gsp视图/文件中创建了一个按钮

<button type="button">PDF Me!</button>

ByteArrayOutputStream bytes = pdfRenderingService.render(template: "/pdfs/report", model: [data: data])

在视图中绑定图像

<rendering:inlinePng bytes="${imageBytes}" class="some-class" />

模型数据是domainInstance,如何将该按钮与此renderpdf连接?

可能是我应该更多地指定我的代码

def invoice ={
    def vermittlungInstance = Vermittlung.get(params.id)


    def aa = vermittlungInstance.lieferungen.id
    def lieferungInstance = Lieferung.get(aa)

    def bb = lieferungInstance.packete.id // .id
    def packetInstance = Packet.findAllByIdInList(bb)

    if (!vermittlungInstance & !lieferungInstance) {
        flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'vermittlung.label', default: 'Vermittlung'), params.id])}"
        redirect(action: "list")
    }
    else {
        if(vermittlungInstance.rechnungen.id!=null || vermittlungInstance.lieferungen.id!=null || lieferungInstance.packete.id!=null ){
            def a = vermittlungInstance.rechnungen.id
            def rechnungList = Rechnung.findById(a)

            def b = vermittlungInstance.lieferungen.id
            def lieferungList = Lieferung.findById(b)

            def c = lieferungInstance.packete.id
            //println c
            def packetList = Packet.findAllByIdInList(c)//findById(c)

            def d = packetInstance.artikel.id//id
            def artikelList = Artikel.findAllByIdInList(d)//findById(d)

            def e = lieferungInstance.adressen.id
            def adresseList = Adresse.findById(e)

            [vermittlungInstance: vermittlungInstance,
                    rechnungInstanceList:rechnungList,
                    lieferungInstanceList:lieferungList,
                    packetInstanceList: packetList,
                    artikelInstanceList: artikelList,
                    adresseInstanceList: adresseList
            ]



            //System.out.println(c)

        }

        else{

            def rechnungList = Rechnung.all
            def lieferungList = Lieferung.all
            def packetList = Packet.all
            def artikelList = Artikel.all
            def adresseList = Adresse.all

            [vermittlungInstance: vermittlungInstance,
                    rechnungInstanceList:rechnungList,
                    lieferungInstanceList:lieferungList,
                    packetInstanceList: packetList,
                    artikelInstanceList: artikelList,
                    adresseInstanceList: adresseList
            ]
        }



    }

}

这是我在控制器中的def,我试图把这个renderpdf放在很多地方,但是它不会渲染页面,实际上我在html(浏览器)中改变了一些值,所以它应该用html渲染。 / p>

控制器似乎是renderpdf的错误位置,但.gsp没有渲染函数

感谢

2 个答案:

答案 0 :(得分:4)

添加新操作,生成发票的pdf版本并从您的视图中链接它们。

这是您的链接:

<g:link action="downloadInvoice" id="${yourInvoiceID}">Download invoice</g:link>

在你的控制中添加以下内容:

def downloadInvoice = {
    def invoice = Invoice.get(params.id) //replace with your logic

   renderPdf(template: '/templates/pdf/invoice', model: [invoice: invoice], filename: "yourTitle")
} 

您的发票模板是一个简单的gsp视图,您可以在其中放置所有HTML(包括图像)和CSS:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
    <head>
        <title>Invoice</title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <link rel="stylesheet" href="${resource(dir:'css',file:'your.css')}" />        
    </head>
    <body>
        <img src="${resource(dir:'images',file:'invoiceLogo.png')}" />
        <h1>Invoice: ${invoice.id}</h1>
         .
         .
         .
    </body>
</html>    

希望这个例子有帮助!

答案 1 :(得分:1)

如何将unicode字体添加到此插件? unicode字符不会在呈现的pdf中显示。生成的pdf包含空格代替unicode字符,尽管它们显示在其他gsp页面中。后来我尝试了下面的CSS。但没有奏效。

@font-face {
  font-family: 'Ubuntu';
  font-style: normal;
  font-weight: 400;
  src:url(http://themes.googleusercontent.com/static/fonts/ubuntu/v4/_xyN3apAT_yRRDeqB3sPRg.woff) format('woff');
  -fs-pdf-font-embed: embed;
  -fs-pdf-font-encoding: UTF-8;
}

body pre{
font-size: 14px;
font-family: 'Ubuntu', Helvetica, Arial, sans-serif;
}

谢谢,