HTML2PDF conversion issue

Last post 04-13-2011, 11:49 AM by Hlybbi. 3 replies.
Sort Posts: Previous Next
  •  02-20-2008, 5:25 PM 37203

    HTML2PDF conversion issue

    I have this code (c#) to attach a PDF created from HTML on the fly to an email.  objMessage is a System.Net.Mail.Mailmessage object. 
     

    CuteEditor.Convertor.PDF.HTML2PDF objConverter = new CuteEditor.Convertor.PDF.HTML2PDF("<html><body><b>this is a test</b></body></html>");

    Stream sPDFForm = new MemoryStream();

    objConverter.RenderSinglePageContent = false;

    objConverter.Render();

    objConverter.Save(sPDFForm);

     

    objMessage.Attachments.Add(new System.Net.Mail.Attachment(sPDFForm, "a.pdf"));
     
    When I send the message, I receive the email with the attachment, however when I try to open it I get an error from adobe:
     
    "Adobe Reader could not open 'a.pdf' because it is either not a supported file type or because the file has been damaged (for example, it was sent as an email attachment and wasn't correctly decoded)."
     
    any ideas?
     
    Thanks
     
    Adam
  •  02-20-2008, 5:26 PM 37204 in reply to 37203

    Re: HTML2PDF conversion issue

    I should also point out that if I use the exact same code to create the PDF, except save it as a file, the file is fine.
  •  02-20-2008, 7:59 PM 37207 in reply to 37203

    Re: HTML2PDF conversion issue

    And I have come full circle and answered my own question after banging my head for hours.... 
     
    For those who care, the final code that I got to work was:
     
    -------------------------------------------------------------------------------------------

    CuteEditor.Convertor.PDF.
    HTML2PDF objConverter = new CuteEditor.Convertor.PDF.HTML2PDF(strFormContent);

    Stream sPDFForm = new MemoryStream();

    objConverter.RenderSinglePageContent = false;

    objConverter.Render();

    objConverter.Save(sPDFForm);

    sPDFForm.Position = 0;

    System.Net.Mail.Attachment objAttachment = new System.Net.Mail.Attachment(sPDFForm, objContent.ContentHeader + ".pdf");

     

    objMessage.Attachments.Add(objAttachment);

    ------------------------------------------------------------------------------------------------------------------------------------------
    What I didn't catch was that after calling the 'save' method of the HTML2PDF object, the stream position is at the end instead of the beginning.  This explains why I got zero byte files.  Once I added the line:
     
    sPDFForm.Position = 0;
     
    to set the position back to the beginning of the stream, all was working.
     
    Hope this helps someone out there!
     
    Adam (not that Adam!)
     
     
  •  04-13-2011, 11:49 AM 67151 in reply to 37207

    Re: HTML2PDF conversion issue

    Thank you,
    this was making me crazy
View as RSS news feed in XML