관련
에서 XHTML과 PDF를 생성, 내가 PDF로 쉽게 HTML/XHTML을 변환 할 수 있습니다.
이제 HTML/XHTML 용 매개 변수를 설정하여 핵심 JSTL로 템플릿을 작성하기 위해 JSP를 사용하기로 결정했습니다. Tomcat은 JSP를 HTML로 구문 분석하므로 결과를 가져 와서 응답 내용을 가져 와서 Flying-saucer를 통해 PDF로 변환하려고합니다.
그러나 위 질문과 관련하여 응답 내용을 차단하는 방법을 모르겠습니다.
1 - ServletResponse 인터페이스는 JSP를 처리하기 전에 내용을 추가하는 데 유용합니다.
2 - HttpServletResponse는 응답의 헤더를 수정하는 것입니다.
내 메서드 내에서 응답의 내용을 가져 와서 비행 접시로 처리 한 다음 클라이언트에 "application/pdf"로 다시 보내려면 어떻게해야합니까?
N.B. : 우리는 Struts 1을 사용하고 있습니다. 나쁘다는 것을 알고 있습니다. 곧이 응용 프로그램을 위해 아직 봄으로 이동할 계획입니다.
하지 않음 - 더 - 최고하지만 일하는 솔루션 : 지금은 URI를 통해 JSP로의 내용을 설정하기위한 요청 매개 변수를 사용합니다 들어
후 "응용 프로그램/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¶m2=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);
}
}
를 사용하여 뷰를 렌더링 받아야 작동합니다. 영감을 얻으려면 [struts2-pdfstream] (https://github.com/aleksandr-m/struts2-pdfstream)을 참조하십시오. –