微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

java-Spring AbstractPdfView显示已经在浏览器中创建了pdf

我已经创建了2个pdf文件.它们位于以下文件夹中WebContent / pdf /

我已经扩展了Spring的AbstractPdfView,以便即时生成pdf.

这次我想用它来

1)显示已创建的pdf,
2)使用itext将模型对象传递给第二个pdf,并填写已经创建的pdf表单字段.

我知道1)我可以创建一个链接并直接访问pdf.我试图通过扩展AbstractPdfView访问它,因为我认为我需要在情况2中使用它.

我只是不确定如何获取资源,然后使用此类在浏览器中显示资源.

谁能请我示范如何完成此工作?

弹簧-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 id="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>
最佳答案
我认为您实际上可能想继承AbstractpdfstamperView的子类:

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

看起来您将AbstractpdfstamperView子类的“ url”属性设置为现有PDF文件的路径:

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

然后,您将需要重写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"));
}

您可能需要查看iText pdfstamper文档,以了解所有可用的选项.

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

原文地址:https://www.jb51.cc/spring/531730.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐