我使用 AbstractPdfView 类生成 pdf 并在浏览器中下载,但我还需要通过电子邮件发送它。我知道解决方案非常明显,但我已经坚持了一天,看不到它。这是我到目前为止所拥有的:
@GetMapping()
public PdfView generate(Model model, @RequestParam int event) {
model.addAttribute("event", eventService.getEventById(event));
return new PdfView();
}
那是 PdfView 类:
public class PdfView extends AbstractPdfView {
@Override
protected void buildPdfDocument(Map<String, Object> model, Document document, PdfWriter writer, HttpServletRequest request, HttpServletResponse response) throws Exception {
//add the iformation for the pdf
document.add(getInvoiceInfo());
}
}
这就是 AbstractPdfView 类:
public abstract class AbstractPdfView extends AbstractView {
public AbstractPdfView() {
setContentType("application/pdf");
}
@Override
protected boolean generatesDownloadContent() {
return true;
}
@Override
protected final void renderMergedOutputModel(Map<String, Object> model, HttpServletRequest request, HttpServletResponse response) throws Exception {
ByteArrayOutputStream out = createTemporaryOutputStream();
Document document = new Document(PageSize.A4.rotate(), 35, 35, 50, 36);
PdfWriter writer = PdfWriter.getInstance(document, out);
prepareWriter(model, writer, request);
buildPdfMetadata(model, document, request);
document.open();
buildPdfDocument(model, document, writer, request, response);
document.close();
//i know i has to happen before this bit here but not sure how
writeToResponse(response, out);
}
protected void prepareWriter(Map<String, Object> model, PdfWriter writer, HttpServletRequest request) throws DocumentException {
writer.setViewerPreferences(getViewerPreferences());
}
protected int getViewerPreferences() {
return PdfWriter.ALLOW_PRINTING | PdfWriter.PageLayoutSinglePage;
}
protected void buildPdfMetadata(Map<String, Object> model, Document document, HttpServletRequest request) {
}
欢迎任何帮助!感谢你们。
饮歌长啸
相关分类