猿问

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

我使用 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) {

   }


欢迎任何帮助!感谢你们。


喵喵时光机
浏览 142回答 1
1回答

饮歌长啸

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

相关分类

Java
我要回答