Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Doogie Talons 183 posts 318 karma points
    May 20, 2016 @ 15:25
    Doogie Talons
    0

    EMAIL PDF as an attachment.

    With this package, is it possible to email the final PDF as an attached file rather than render inside the browser.

    Doogie

  • Biagio Paruolo 1618 posts 1910 karma points c-trib
    May 21, 2016 @ 13:31
    Biagio Paruolo
    0

    Do you need only attach the pdf file or generate, attach and send?

  • Doogie Talons 183 posts 318 karma points
    May 26, 2016 @ 09:03
    Doogie Talons
    0

    Basically I just need the pdf to send. Once a questionnaire is filled out a report is to be emailed as a pdf to the persons who filled it in. The report relates to certain umbraco nodes etc. It's simple enough to download but it's a data mining excersise and the pretext is give us your email get a free report... if we don't mail it and just allow a download then there's no pretext so to speak.

  • Darren Ferguson 1022 posts 3259 karma points MVP c-trib
    May 21, 2016 @ 17:41
    Darren Ferguson
    0

    You'd need to write the code to save it to disc and send it as an attachment, which should be pretty straightforward - but it isn't part of the package.

  • Doogie Talons 183 posts 318 karma points
    May 26, 2016 @ 08:56
    Doogie Talons
    0

    Aaah that's the problem I can see a PDF generated in the browser but no idea on how to save it to the server. I guess once it's saved and I know where it is a simple script could mail it but the step whereby it saves rather than renders eludes me.

  • Darren Ferguson 1022 posts 3259 karma points MVP c-trib
    May 26, 2016 @ 09:22
    Darren Ferguson
    0

    You'd need to do something like this:

    http://www.csharp-examples.net/download-files/

    From your own code, request the PDF URL and save it to disc (in a temp folder), then write the c# code to send an email and attach it.

    Does that help at all?

    Thanks.

  • Doogie Talons 183 posts 318 karma points
    Jun 08, 2016 @ 17:48
    Doogie Talons
    0

    Aaah I can't get this to work without showing the PDF to the browser.

  • Doogie Talons 183 posts 318 karma points
    Jun 14, 2016 @ 14:45
    Doogie Talons
    0

    OK I am stuck on this so I think I need to step back and understand what it is this package does.

    My goal is to simply email the PDF without exposing it to the browser. But where is the PDF at what point is a PDF created and where is it held. It appears to be in a memory stream but at what point can I access it and send it on it's way.

    I can either mail it from the memory stream but don't know how to reference it. Or I can save it to a folder on the server and mail it from there, but again I don't know how to consolidate it and save it.

    Has anyone done anything with this package other than render the PDF to the browser or download the pdf ?

  • Biagio Paruolo 1618 posts 1910 karma points c-trib
    Jun 15, 2016 @ 06:19
    Biagio Paruolo
    100

    Hi,

    I suggest to read this article and project

    http://www.codeproject.com/Articles/570682/PDF-File-Writer-Csharp-Class-Library-Version

    I use this code to download the generated pdf stream. PDFGeneratorDocuments is a my class to generate the PDF with MigraDoc. The important line is return File to download stream file.

      PDFGeneratorDocuments PDFGeneratorDocumentsManager = new PDFGeneratorDocuments();
    
                    var memStream = PDFGeneratorDocumentsManager.GetDDTinPDF(stampaView);
    
                    string nomefile = stampaView.HeaderDDT.NumMov + ".pdf";
                    return File(memStream.ToArray(), "application/pdf", nomefile);
    

    and this one to attach to email ( I use System.Net.Mail )

    bool emailHtml = true;
    string filePath = PDFHelper.ElaboraDatiToPDF(dataInfo); //path to PDF file on disk
    
                        try
                        {
                            MailMessage mail = new MailMessage();
                            SmtpClient SmtpServer = new SmtpClient();
    
                            mail.To.Add(emailTo);
                            mail.Subject = emailSubject;
                            mail.Body = emailBody;
                            mail.IsBodyHtml = emailHtml;
    
                            System.Net.Mail.Attachment attachment;
                            attachment = new System.Net.Mail.Attachment(filePath);
                            mail.Attachments.Add(attachment);
    
                            SmtpServer.Send(mail);
    
                        }
                        catch (Exception ex)
                        {
                            Log.Add(LogTypes.Error, 0, "Error send email " + ex.Message);
                        }
    

    Bye

  • Doogie Talons 183 posts 318 karma points
    Jun 15, 2016 @ 07:57
    Doogie Talons
    0

    Thanks for that using similar principles I have managed on first instance to make PDF Generator package do what I need.

    I have a ways to go put in some proper checks and balances but this is what I have come up with that works in Razor and doesn't stress the server.

      @inherits Umbraco.Web.Mvc.UmbracoTemplatePage 
        @using System.Net; 
        @using System.Net.Mail;
    
    
        @if (emailedV) { <p>Emailed</p>      } @if (!emailedV) { <p>Not Emailed</p>  }
    
        @functions{
    
            private bool emailedV;  
            private bool downloadedV;
            string pdfUrl;  
            private MemoryStream pdfMemoryStream;
    
    
    protected override void InitializePage() {
    
            pdfUrl = @"http://wherethecodeisthatmakesthepdf.com/url";
           WebClient client = new WebClient();    
              try 
                    {
                        pdfMemoryStream = new MemoryStream(client.DownloadData(pdfUrl));
                    } 
                    finally 
                    {
                        client.Dispose();
                    }
    
    
    
                 try
                    {
    
                    MailMessage mail = new MailMessage();
    
    
                    mail.From = new MailAddress("from@address");
                    mail.To.Add("to@address");
                    mail.Subject = "Your Document";
                    mail.IsBodyHtml = false;
                    mail.Body = "healthcheck"; 
    mail.Attachments.Add(new System.Net.Mail.Attachment(pdfMemoryStream, "yourdocument.pdf"));  
    
                    SmtpClient smtp = new SmtpClient();
                    smtp.Send(mail);
                    mail.Attachments.Dispose(); 
    
                    }
                    catch(Exception ex)
                    {
                    emailedV = false;
                    }   emailedV = true; }
    
        }
    
Please Sign in or register to post replies

Write your reply to:

Draft