Spring AbstractPdfView 在浏览器中显示已经创建的pdf

发布于 2024-11-29 06:14:31 字数 1188 浏览 1 评论 0 原文

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

我之前已经扩展了 Spring 的 AbstractPdfView 来动态制作 pdf。

这次我想用它来

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

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

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

谁能告诉我如何通过示例来完成此任务?

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

慕巷 2024-12-06 06:14:31

我认为您实际上可能想继承 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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文