1

我想从 Spring MVC 中的模板(使用此处所示的 pdf 表单)创建 PDF并将其输出到浏览器,但AbstractPdfView我不知道如何Document从.. ? 以下是我到目前为止的代码,谢谢PdfStamperPdfWriter

public class RecipePdf extends AbstractPdfView {

@Override
protected void buildPdfDocument(Map<String, Object> model, Document document,
        PdfWriter pdfWriter, HttpServletRequest request, HttpServletResponse response)
        throws Exception {

    PdfReader pdfTemplate = new PdfReader ("/WEB-INF/template/recipe.pdf");
    FileOutputStream fileOutputStream = new FileOutputStream("test.pdf");
    PdfStamper stamper = new PdfStamper(pdfTemplate, fileOutputStream);
    stamper.setFormFlattening(true);

    stamper.getAcroFields().setField("number", "12345");

    stamper.close();
    pdfTemplate.close();
4

2 回答 2

5

Spring 的 AbstractPdfStamperView 和 iText 可用于使用预定义模板生成 PDF。

package com.pdf.view;

import java.util.Locale;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.context.MessageSource;
import org.springframework.context.MessageSourceAware;
import org.springframework.web.servlet.view.document.AbstractPdfStamperView;

import com.lowagie.text.pdf.PdfStamper;

public class PDFView extends AbstractPdfStamperView implements
        MessageSourceAware {

    private MessageSource messageSource;

    @Override
    public void setMessageSource(MessageSource messageSource) {
        this.messageSource = messageSource;
    }

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

        stamper.setFormFlattening(true);
            String customerName = (String) model.get("customerName");

        Locale locale = request.getLocale();
        stamper.getAcroFields().setField("customerNameLabel",
                messageSource.getMessage("label.customername", null, locale));

        stamper.getAcroFields().setField("customerNameValue", customerName);

        stamper.close();
    }
}

我使用了 XMLViewResolver。下面是 spring-servlet.xml 中的条目:

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

下面是 spring-pdf-views.xml 的内容

<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.1.xsd">

    <bean id="invoicePDF"
        class="com.pdf.view.PDFView">
        <property name="url" value = "/WEB-INF/resources/templates/invoiceTemplate.pdf" />
    </bean>
 </beans>

下面是控制器代码中定义的方法,它将呈现视图。此处 ModelAndView 中设置的视图名称与上面 spring-pdf-views.xml 中定义的相同。

@RequestMapping(value = "/pdf",method = RequestMethod.POST)
public ModelAndView renderPDF(HttpServletRequest request,
        HttpServletResponse response) throws Exception {

        ModelAndView mav = new ModelAndView("invoicePDF");
        mav.addObject("customerName", "XYZ"); 
        return mav;
    }
于 2014-05-17T07:52:25.017 回答
1

1)我在本地开发我的文档生成器并使用 jUnit 测试它们 2)如果您只需要 PDF,那么它应该在输出目录中...但是... 3)但是...如果您需要加入该文档成一个更大的文档,然后使用类似以下的内容阅读它:

    Document document = new Document();

    PdfWriter writer = PdfWriter.getInstance(document, outputStream);

    document.open();
    PdfContentByte cb = writer.getDirectContent();

    for (String pdfFileName : pdfFileNamesList) {
        InputStream in = new FileInputStream(pdfFileName);
        PdfReader reader = new PdfReader(in);
        int numberOfPages = reader.getNumberOfPages();
        for (int i = 1; i <= numberOfPages; i++) {
            document.newPage();
            //import the page from source pdf
            PdfImportedPage page = writer.getImportedPage(reader, i);
            //add the page to the destination pdf
            cb.addTemplate(page, 0, 0);
        }
     }
于 2013-04-08T16:35:25.993 回答