开发者

Spring AbstractPdfView display already created pdf in the browser

I have 2 pdf files already created. They are in the following folder WebContent/pdf/

I have extended Spring's AbstractPdfView before to make a pdf on the fly.

This time I want to use it to

1) display an already created pdf and 2) use itext to pass a model object to the second pdf and fill already created pdf form fields.

I know with 1) I can just create a link and access the pdf directly. I was trying to access it via extending AbstractPdfView since I believe that I need to use that for case 2).

I am just not sure how to get the resource and then display it in the browser with this class.

Can anyone please show me how to accomplish this with a sample?

spring-pdf-views.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

    <bean id="BlankPDF" class="com.example.BlankPDF"/>
    <bean i开发者_运维百科d="PopulatedPDF" class="com.example.PopulatedPDF"/>

</beans>

spring-servlet.xml

<bean id="xmlViewResolver" class="org.springframework.web.servlet.view.XmlViewResolver">
    <property name="order" value="1"/>
    <property name="location">
        <value>/WEB-INF/spring-pdf-views.xml</value>
    </property>
</bean>


I think you may actually want to subclass AbstractPdfStamperView instead:

http://static.springsource.org/spring/docs/3.0.x/api/org/springframework/web/servlet/view/document/AbstractPdfStamperView.html

It looks like you set the "url" property of your AbstractPdfStamperView subclass to be the path to your existing PDF file:

<!-- PopulatedPDF extends AbstractPdfStamperView -->
<bean id="PopulatedPdf class="com.example.PopulatedPdf"> 
    <property name="url" value="/WEB-INF/pdfs/blankform.pdf" />
</bean>

Then you will need to override mergePdfDocument():

@Override
protected void mergePdfDocument(Map<String,Object> model,
                                     com.lowagie.text.pdf.PdfStamper stamper,
                                     HttpServletRequest request,
                                     HttpServletResponse response)
                              throws Exception {

    // follow example code for filling out a form using iText:
    // http://itextpdf.com/examples/iia.php?id=122

    AcroFields form = stamper.getAcroFields();
    // form.setField("fieldName", model.get("fieldName"));
}

You probably will want to look at the iText PdfStamper docs to figure out what all your options are there.

http://api.itextpdf.com/itext/index.html?com/itextpdf/text/pdf/PdfStamper.html

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜