为了账号安全,请及时绑定邮箱和手机立即绑定

从 Java 中的 AbstractPdfView 通过电子邮件发送 PDF

从 Java 中的 AbstractPdfView 通过电子邮件发送 PDF

喵喵时光机 2022-11-02 10:18:38
我使用 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) {   }欢迎任何帮助!感谢你们。
查看完整描述

1 回答

?
饮歌长啸

TA贡献1951条经验 获得超3个赞

好吧,我已经完成了。如果有人遇到此问题,请在此处发布。我基本上在 AbstractPdfView 的实现中自动连接 emailService,用它调用抽象类的构造函数。这里也是一个很好的阅读主题:https ://www.baeldung.com/spring-autowired-abstract-class


查看完整回答
反对 回复 2022-11-02
  • 1 回答
  • 0 关注
  • 141 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信