Convert HTML to PDF creates damaged PDF

  •  12-05-2007, 2:32 AM

    Convert HTML to PDF creates damaged PDF

    I am trying to create a PDF and stream it directly to my browser. The PDF gets created, but when Acrobat opens it, it says (very briefly) "The file is damaged but is being repaired". For a simple file, this seems to work, but for something more compex, what comes out can't be read.
     
    I attach a code snippet, which has some code commenetd out. If you swap around the commented out code, you will see an example of "bad" output.
     
    The code can be modified further to only stream the portion of the buffer in use, but that makes no difference.
     
    The code is loosely based on the example in the documentation under the heading "Dynamic PDF Creation" (which incidentelly has an error in it: [string t= t= "<html><body>" + yourstring+ "</body></html>";]
     
    Regards
     
    Paul
     
     
    >>

    protected void Page_Load(object sender, EventArgs e)

    {

    string docName = "Test.pdf";

    //

    // Un comment this and replace the line below

    //

    //string tableDef =

    // "<table style=\"width:75%; border-right: gray 1px outset; border-top: gray 1px outset; border-left: gray 1px outset;" +

    // "border-bottom: gray 1px outset; border-collapse: collapse; background-color: white\">" +

    // "<tr>" +

    // "<td colspan=\"2\" style=\"border-right: gray 1px inset; padding-right: 5px; border-top: gray 1px inset;" +

    // "padding-left: 5px; font-size: 12pt; padding-bottom: 5px; border-left: gray 1px inset;" +

    // "padding-top: 5px; border-bottom: gray 1px inset; font-family: Times New Roman;" +

    // "background-color: white; text-align: center;\">" +

    // "Hello Adam!</td>" +

    // "</tr>" +

    //"</table>";

    string tableDef = "Hello Adam";

    MemoryStream x = new MemoryStream();

    string t = "<html><body>" + tableDef + "</body></html>";

    CuteEditor.Convertor.PDF.HTML2PDF html2pdf = new CuteEditor.Convertor.PDF.HTML2PDF(t);

    html2pdf.Render();

    html2pdf.Save(x);

    HttpResponse resp = HttpContext.Current.Response;

    resp.ContentType = "application / pdf";

    resp.AddHeader("Content-Disposition", "attachment; filename = " + docName);

    resp.BinaryWrite(x.GetBuffer());

    resp.End();

    }

     
     
     
View Complete Thread