Convert HTML to PDF creates damaged PDF

Last post 06-04-2013, 2:43 AM by recthor. 5 replies.
Sort Posts: Previous Next
  •  12-05-2007, 2:32 AM 35655

    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();

    }

     
     
     
  •  12-06-2007, 10:00 AM 35710 in reply to 35655

    Re: Convert HTML to PDF creates damaged PDF

  •  12-06-2007, 1:22 PM 35716 in reply to 35710

    Re: Convert HTML to PDF creates damaged PDF

    Hi Adam,
     
    The code in that thread is practically identical to mine. However, I have reworked my code to be in line with that thread and it makes no difference.
     
    For the "simple" piece of text "Hello Adam", the pdf is written, but Adobe complains that it is damaged. For the more complicated version of the text (the table), it just comes out as a lrge "blob" as it did before.
     
    Here is my modified code:
     

    string docName = "Test.pdf";

    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(Server.HtmlDecode(t));

    html2pdf.Render();

    html2pdf.Save(x);

     

    HttpResponse resp = HttpContext.Current.Response;

    resp.ContentType = "application / pdf";

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

    x.WriteTo(resp.OutputStream);

    resp.End();

  •  12-16-2007, 9:56 AM 35828 in reply to 35716

    Re: Convert HTML to PDF creates damaged PDF

    Hi Adam,
     
    Is there any update on this ?
     
    Regards
     
    Paul
  •  02-20-2008, 8:04 PM 37208 in reply to 35828

    Re: Convert HTML to PDF creates damaged PDF

    Hey,
     
    I just had a very similar issue and found the solution after a few hours of banging my head.  After looking at your code, I think my solution would work for you too
     
     
    In the above example, after you call the save method, add the line:
     
    x.Position = 0;
     
     
    Hope that helps guys...
     
  •  06-04-2013, 2:43 AM 77526 in reply to 35655

    Re: Convert HTML to PDF creates damaged PDF

    If you wanna save these complicated steps and find a easy way, you can google html to pdf Converter and you will find many useful tools, or google how to convert html to pdf and vice versa to get instructive articles. Good luck:) I think PDF conversion programmes can enable to quickly convert files. i have use PDF converter which can original layouts hyperlinks, Images and tables retained in word ,text, image, Epub, html.but i forgot download the wedsite ,go to google. By the way , pay attention to your system platform, select the suitable one . I use a PDF converter driver found on the internet . Install it and it becomes a selectable converter option.Then you can convert PDFs to many forms  in any program at all, including Adobe Acrobat . Just open a PDF, select convert, and choice a form you want, then you can do pdf to html conversion and vice versa,the task will be finished in several seconds. if you haven't found a good choice  , you can have a try. best wishes.
View as RSS news feed in XML