Emailing Dynamically Created PDFs

Emailing Dynamically Created PDFs

Support · Oct 25, 2005
Having the ability to create great looking, DynamicPDF documents is one thing but we sometimes need those documents to get to an end user, a customer or a colleague in real time. Well, here is some helpful information that will allow you to email those PDFs from within your DynamicPDF applications. There are three basic steps for getting your PDFs emailed on the fly: 1. Create your PDF using DynamicPDF Generator for COM/ActiveX. 2. Using the "DrawToFile" method save that PDF to File. 3. Using the emailing functionality of the CDOSYS Library attach and send the PDF. First, create a PDF. If you are just getting started you can take a look at any of our Generator for COM/ActiveX examples for creating DynamicPDFs, http://www.DynamicPDF.com/Products/Generator/Examples.csp Second, you will need to use the "DrawToFile" method and save the PDF to an actual location on disk. Saving the file to an actual location on disk is required using CDOSYS. (There are third party tools out there that will allow you to attach a file from a memory stream if required.) Finally, use the "CDO.Message" object to create, attached and send the email. Create the "CDO.Message" object and then set its properties (to, from, body, etc.). Then add an attachment to the message and send it. Here is some sample code for ASP: Dim objDoc Set objDoc = Server.CreateObject("DPDF_Gen.Document") 'Create DynamicPDF Document Here objDoc.DrawToFile "C:\temp\document.pdf" Dim ObjSendMail Set ObjSendMail = CreateObject("CDO.Message") ObjSendMail.To = "receiver@domain.com" ObjSendMail.Subject = "Attached PDF Email" ObjSendMail.From = "sender@domain.com" ObjSendMail.TextBody = "This is the body of the email. The DynamicPDF is attached" ObjSendMail.AddAttachment "C:\temp\document.pdf" ObjSendMail.Send

Re:Emailing Dynamically Created PDFs

Forum member · Oct 25, 2005
Adobe Acrobat Reader v7 supports emailing of the pdf directly. However, the nature of the generated PDF means that, when Acrobat trys to attach it to an email, it actually attaches the asp page, and not the pdf file itself - as the pdf is displayed within the asp page. However, I understand from cete support that v3 of ReportGenerator com/activex will allow the pdf to be opened directly in Acrobat (as with the current .NET version), thereby allowing direct emailing of the PDF file. Thanks Craig Johnson

Re:Emailing Dynamically Created PDFs

Support · Oct 25, 2005
Craig, Yes, when using the Acrobat emailing feature to email a document, the email attachment will always receive the file extension from the page that is was created in. Therefore if the PDF was created in an ASP page then the email attachment for the PDF will actually have an ASP extension. To get around this you actually have three options. First, you could change the Acrobat Preferences and specify to not open the PDF in the browser window (in Acrobat 7 go to Edit--> Preferences--> Internet then under "Web Browser Options" uncheck the "Display PDF in browser"). Now if you hit the ASP page again and generate the PDF it will be opened in an actual instance of Acrobat (not in the browser plug-in) and the PDF file extension will be used if the email button is pressed from within Acrobat. This would have to be repeated on any machine that wanted this functionality. The second option would be to actually save the dynamicPDF to disk first and then redirect to the file, instead of streaming the PDF directly to the end user's browser from the ASP page. This would then mean that even if the PDF is opened in the Acrobat plug-in the document would be an actual PDF extension. The ASP code for saving the PDF to file and then redirecting to it would look something like this: objReport.DrawToFile( "C:\weblocation\MyPDF.pdf" ) Response.Redirect( "http://weblocation/MyPDF.pdf" ) (Please note that using the above method you may need to modify file permissions on the server. You may also need to add additional security features as anyone who knows the URL will then be able to access the PDF.) The third option would be to force the end user to download the PDF. Using this method the end user will be prompted with a dialog box and they will have the choice to “Open” or “Save” the PDF. The ASP code for forcing this would be something like: Response.ContentType = "application/pdf" Response.AddHeader "content-disposition", "attachment; filename=MyPDF.pdf" Response.BinaryWrite objReport.Draw Using any of the three above methods will allow you to attach the PDF document (as a PDF) using the email feature from within Acrobat. Thanks, ceTe Software Support Team

Re:Emailing Dynamically Created PDFs

Forum member · Oct 26, 2005
Hi Option 3 looks to most appealing. Does this involve firstly saving the pdf, and then calling it to display, or can the open/save routing be incorporated into the pdf page?

Re:Emailing Dynamically Created PDFs

Support · Oct 26, 2005
Using option 3 above does not require saving the PDF first. The explanation is this: 'this line specifies the open/save dialog box to be displayed Response.AddHeader "content-disposition", "attachment; filename=MyPDF.pdf" 'this line takes the byte array returned from the draw method and writes it to the response object Response.BinaryWrite objReport.Draw No temporary saving of the PDF is needed. Thanks, ceTe Software Support Team

Re:Emailing Dynamically Created PDFs

Forum member · Oct 26, 2005
Hi Great. So how do I incorporate this into my existing pdf stored procedure pages... <!-- METADATA TYPE="typelib" UUID="{567E47DC-8220-403F-B348-EB9A035648BB}" --> <!-- METADATA TYPE="typelib" UUID="{00000206-0000-0010-8000-00AA006D2EA4}" --> <% Option Explicit Dim plancondition, plancomments, objReport, showColor showColor = True call Main() Sub Main().........

Re:Emailing Dynamically Created PDFs

Support · Oct 26, 2005
Replace the objReport.DrawToASP method with the code from above. It will look something like this: 'objReport.DrawToASP Response.ContentType = "application/pdf" Response.AddHeader "content-disposition", "attachment; filename=MyPDF.pdf" Response.BinaryWrite objReport.Draw If you need further help with the placement of the code snippet, please send your code to support@cete.com. Thanks, ceTe Software Support Team

Re:Emailing Dynamically Created PDFs

Forum member · Oct 27, 2005
That worked perfectly - many thanks! Craig