2014-11-03 133 views
2

我試圖發送PDF文件作爲電子郵件中的附件。我不想在服務器或客戶機上保存pdf文件。我試過以下。但它不起作用。請建議正確的方法來做到這一點。使用grails郵件插件發送電子郵件中的PDF附件

ByteArrayOutputStream bytes = pdfRenderingService.render(template: "path to template", model: [account:account]) 

asynchronousMailService.sendMail { 
        multipart true 
        to emailID 
        subject emailSubject 
        html emailBodyContent 
        attachBytes "filename.pdf", "application/pdf", bytes 
       } 

它不起作用,因爲它需要字節發送附件。並且ByteArrayOutputStream不以字節爲單位給出輸出。有沒有人有這個解決方案?我試過bytes.toByteArray()。但它給出以下例外:

2014-11-03/22:25:35.994 [http-bio-8080-exec-9] ERROR errors.GrailsExceptionResolver - MissingMethodException occurred when processing request: [GET] /morningProc/approvalPdfFR 
No signature of method: [B.call() is applicable for argument types: (java.lang.String, java.lang.String, [B) values: [Approval.pdf, application/pdf, [37, 80, ...]] 
Possible solutions: wait(), any(), grep(), dump(), find(), collect(). Stacktrace follows: 
groovy.lang.MissingMethodException: No signature of method: [B.call() is applicable for argument types: (java.lang.String, java.lang.String, [B) values: [Approval.pdf, application/pdf, [37, 80, ...]] 
Possible solutions: wait(), any(), grep(), dump(), find(), collect() 
    at com.crm.MultilingualEmailService$_$tt__sendEmailFormateWithAttachment_closure14$$EOuZNrCU.doCall(MultilingualEmailService.groovy:79) 
    at grails.plugin.mail.MailService.sendMail(MailService.groovy:39) 
    at MailGrailsPlugin$_configureSendMail_closure7.doCall(MailGrailsPlugin.groovy:180) 
    at com.crm.MultilingualEmailService$$EOuZNrCS.$tt__sendEmailFormateWithAttachment(MultilingualEmailService.groovy:74) 
    at com.crm.MorningProcController$$EOuZVK1N.approvalPdfFR(MorningProcController.groovy:882) 
    at grails.plugin.cache.web.filter.PageFragmentCachingFilter.doFilter(PageFragmentCachingFilter.java:200) 
    at grails.plugin.cache.web.filter.AbstractFilter.doFilter(AbstractFilter.java:63) 
    at grails.plugin.springsecurity.web.filter.GrailsAnonymousAuthenticationFilter.doFilter(GrailsAnonymousAuthenticationFilter.java:53) 
    at grails.plugin.springsecurity.web.authentication.RequestHolderAuthenticationFilter.doFilter(RequestHolderAuthenticationFilter.java:49) 
    at grails.plugin.springsecurity.web.authentication.logout.MutableLogoutFilter.doFilter(MutableLogoutFilter.java:82) 
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) 
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) 
    at java.lang.Thread.run(Thread.java:722) 

即使我嘗試使用Wkhtmltopdf插件。但它給了錯誤。 我試過下面的代碼:

byte[] pdfData = wkhtmltoxService.makePdf(
      view: "/morningProc/approvalPdfFR", 
      model: [account:accountInst], 
      header: "", 
      footer: "", 
      marginLeft: 20, 
      marginTop: 35, 
      marginBottom: 20, 
      marginRight: 20, 
      headerSpacing: 10 
    ) 

但不幸的是沒有運氣。它引發異常:

Cannot find wkhtml executable at [:] trying to make it available with the makeBinaryAvailableClosure 
2014-11-04/00:50:35.985 [http-bio-8080-exec-2] ERROR errors.GrailsExceptionResolver - GroovyCastException occurred when processing request: [GET] /morningProc/approvalPdfFR 
Cannot cast object '[email protected]' with class 'groovy.util.ConfigObject' to class 'groovy.lang.Closure' due to: groovy.lang.GroovyRuntimeException: failed to invoke constructor: public groovy.lang.Closure(java.lang.Object) with arguments: [[:]] reason: java.lang.InstantiationException. Stacktrace follows: 
org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object '[email protected]' with class 'groovy.util.ConfigObject' to class 'groovy.lang.Closure' due to: groovy.lang.GroovyRuntimeException: failed to invoke constructor: public groovy.lang.Closure(java.lang.Object) with arguments: [[:]] reason: java.lang.InstantiationException 
    at org.wkhtmltox.WkhtmltoxService.makePdf(WkhtmltoxService.groovy:56) 
    at org.wkhtmltox.WkhtmltoxService.makePdf(WkhtmltoxService.groovy:35) 
    at com.crm.MorningProcController$$EOua5loZ.approvalPdfFR(MorningProcController.groovy:881) 
    at grails.plugin.cache.web.filter.PageFragmentCachingFilter.doFilter(PageFragmentCachingFilter.java:200) 
    at grails.plugin.cache.web.filter.AbstractFilter.doFilter(AbstractFilter.java:63) 
    at grails.plugin.springsecurity.web.filter.GrailsAnonymousAuthenticationFilter.doFilter(GrailsAnonymousAuthenticationFilter.java:53) 
    at grails.plugin.springsecurity.web.authentication.RequestHolderAuthenticationFilter.doFilter(RequestHolderAuthenticationFilter.java:49) 
    at grails.plugin.springsecurity.web.authentication.logout.MutableLogoutFilter.doFilter(MutableLogoutFilter.java:82) 
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) 
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) 
    at java.lang.Thread.run(Thread.java:722) 
+0

嘗試'bytes.toByteArray();' – Jens 2014-11-03 09:26:34

+0

@Jens:這是行不通的。請看我更新的問題。 – user3853055 2014-11-03 09:58:48

+0

呢'attachBytes「filename.pdf」,「應用程序/ PDF」,bytes'工作您使用的是'異步郵件插件1.1'插件 – cfrick 2014-11-03 20:46:10

回答

2

最後它對我有用。以下是解決方案:

ByteArrayOutputStream bytes = pdfRenderingService.render(template: "path to template", model: [account:account]) 

asynchronousMailService.sendMail { 
        multipart true 
        to emailID 
        subject emailSubject 
        html emailBodyContent 
        attachBytes "filename.pdf", "application/pdf", bytes.toByteArray() 
       } 

而且我也運行清理命令。還有一個技巧,不要將字節的變量名稱作爲「attachBytes」。像:attachBytes "filename.pdf", "application/pdf", attachBytes.toByteArray() 這也讓我有很多時間來解決問題。