使用XHTML从已处理的JSP生成PDF

问题描述 投票:0回答:1

Convert a Jsp with css to pdf有关

使用飞碟,我可以轻松地将HTML / XHTML转换为PDF。

现在我希望能够为HTML / XHTML设置参数,所以我决定使用JSP来模拟核心JSTL。 Tomcat将JSP解析为HTML,因此我想通过获取响应内容并通过Flying-saucer处理它来获取结果以将其转换为PDF。

但是,我不知道如何拦截与上述问题相关的响应内容。

1 - ServletResponse接口似乎对在处理JSP之前添加内容很有用。

2 - HttpServletResponse用于修改响应的标头。

如何在我的方法中获取响应的内容,用飞碟处理它,然后将其作为“application / pdf”发送回客户端?

注: :我们正在使用Struts 1,我知道它很糟糕,我们计划很快转移到Spring但尚未用于此应用程序。


不是最好但工作的解决方案:

现在我将使用请求参数通过URI设置Jsp的内容,然后将其作为“application / pdf”发送回客户端。

import org.xhtmlrenderer.pdf.ITextRenderer;
import com.lowagie.text.DocumentException;
import java.io.BufferedReader;
import java.io.CharArrayWriter;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.Scanner;

/**
 * PdfConverter
 * Pdf methods to convert Jsp from URI, or Xhtml/Xml from URI 
 * or internal content to Pdf.
 */
public class PdfConverter {

    private static String encoding = "UTF-8";

    /**
     * Convert JSP from local server to PDF
     * @param jspUri usage :
     *    URL jspUri = 
     *        new URL("http://localhost:8080/myApp/myJspServlet?param1=1&param2=2");
     * @param pdfFileName
     * @return Html content as a String from generated Jsp.
     * @throws DocumentException
     * @throws IOException
     */
    public static String JspToHtmlString(URL jspUri) throws DocumentException, 
                                                            IOException {
        BufferedReader in = new BufferedReader(
            new InputStreamReader(jspUri.openStream())
        );
        CharArrayWriter caw = new CharArrayWriter();
        int octet = 0;
        while ((octet = in.read()) != -1) { caw.write(octet); }
        String pdfContent = caw.toString();
        caw.close();
        return pdfContent;
    }

    /**
     * Convert Xhtml from package to PDF to the folder where this class is 
     * located.
     * @param xhtmlFileName name of the Xhtml file inside the application.
     * @param pdfFileName name of the written Pdf file.
     * @return Pdf file generated and written on disk.
     * @throws DocumentException
     * @throws IOException
     */
    public static File XhtmlToPdfConverter( String xhtmlFileName, 
                                            String pdfFileName) throws
                                                             DocumentException,
                                                             IOException {
        FileOutputStream pdf = new FileOutputStream(pdfFileName);
        new ITextRenderer() {{
            setDocumentFromString(
                new Scanner(
                    this.getClass().getResourceAsStream(xhtmlFileName), encoding
                ).useDelimiter("\\A").next()
            );
            layout();
            createPDF(pdf);
        }};
        pdf.close();
        return new File(pdfFileName);
    }
}
jsp pdf struts-1 flying-saucer
1个回答
0
投票

您可以手动调用请求调度程序来呈现视图。这是一个Spring实现,但这个想法和大多数类应该是相同的。

为了使这个工作你必须以某种方式得到你的ServletContext然后使用虚假的请求/响应渲染视图

// the request url and method arent really relevant, they could be if we wanted to use that information but were not
MockHttpServletRequest request = new MockHttpServletRequest(servletContext, "GET", "/arbitrary/url");
MockHttpServletResponse response = new MockHttpServletResponse();

addModelAsRequestAttributes(request, modelMap);


LocaleResolver localeResolver = new JspLocaleResolver();
localeResolver.setLocale(request, response, Locale.getDefault());
// Push our LocaleResolver into the request
request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE, localeResolver);          

// Push our Locale into the request
LocalizationContext localizationContext = new LocalizationContext(null, Locale.getDefault());
request.setAttribute(Config.FMT_LOCALIZATION_CONTEXT+".request", localizationContext);
request.setAttribute(Config.FMT_LOCALE, Locale.getDefault());

servletContext.getRequestDispatcher("/WEB-INF/pdf_views/mypage.jsp").include(request, response);


return response.getContentAsString();



/*
 * Moves the items in the map to be request.attributes
 */
private void addModelAsRequestAttributes(ServletRequest request, Map<String,Object> modelMap) {
    if(modelMap != null && request != null) {
        for (Map.Entry<String, Object> entry : modelMap.entrySet()) {
            String modelName = entry.getKey();
            Object modelValue = entry.getValue();
            if(modelValue != null) {
                request.setAttribute(modelName, modelValue);
            } else {
                request.removeAttribute(modelName);
            }               
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.