1

I want to make a PDF from a HTML string on memory and send it to a browser. I don't want to save a file on the server so I'm using the following code. I'm also using ITextSharp.

string thistopdf = "<div>ESto es una prueba</div>";

StringReader sr = new StringReader(thistopdf);

Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
using (MemoryStream memoryStream = new MemoryStream())
{            
    PdfWriter writer = PdfWriter.GetInstance(pdfDoc, memoryStream);
    pdfDoc.Open();
    htmlparser.Parse(sr);
    pdfDoc.Close();
    byte[] bytes = memoryStream.ToArray();
    HttpContext.Current.Response.Buffer = false;
    HttpContext.Current.Response.Clear();
    HttpContext.Current.Response.ClearContent();
    HttpContext.Current.Response.ClearHeaders();
    HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment; filename=test.pdf");
    HttpContext.Current.Response.ContentType = "Application/pdf";
    HttpContext.Current.Response.BinaryWrite(bytes);
    HttpContext.Current.Response.Flush();
    HttpContext.Current.Response.End();            
}

The problem is that in my ajax call i get this. the call

   $.ajax({
    type: "GET",
    url: "aglobal.ashx",
    data: {
        comando: "decodeBase64ToPdf"
    }
}).done(function (data, textStatus, req) {
    window.open("data:application/pdf, " + data);

});

and the data i receive would be something like this

%PDF-1.4%����2 0 obj<</Length 70/Filter/FlateDecode>>streamx�+�r�24P�06R%08I�2P�5�%001��%0C%15%0C�Bi\%1A��%����y�%05E��I��!Y@�%06 u�!\�\%002�%10�endstreamendobj4 0 obj<</Type/Page/MediaBox[0 0 595 842]/Resources<</Font<</F1 1 0 R>>>>/Contents 2 0 R/Parent 3 0 R>>endobj1 0 obj<</Type/Font/Subtype/Type1/BaseFont/Helvetica/Encoding/WinAnsiEncoding>>endobj3 0 obj<</Type/Pages/Count 1/Kids[4 0 R]>>endobj5 0 obj<</Type/Catalog/Pages 3 0 R>>endobj6 0 obj<</Producer(iTextSharp� 5.4.4 �2000-2013 1T3XT BVBA \(AGPL-version\))/CreationDate(D:20151005114723-03'00')/ModDate(D:20151005114723-03'00')>>endobjxref0 70000000000 65535 f 0000000263 00000 n 0000000015 00000 n 0000000351 00000 n 0000000151 00000 n 0000000402 00000 n 0000000447 00000 n trailer<</Size 7/Root 5 0 R/Info 6 0 R/ID [<9f72015ead133e6b8d3562d038bf19df><c7c6b8e193f64b5157b8b62e94bc802e>]>>%iText-5.4.4startxref605%%EOF

What am I doing wrong? I cant find a solution to this problem. Is it on the server, or on the JavaScript?

mason
  • 31,774
  • 10
  • 77
  • 121
  • 1
    I think this question has been answered already http://stackoverflow.com/questions/14716425/streaming-a-pdf-to-the-users-browser-using-itextsharp – jonnarosey Oct 05 '15 at 14:58
  • You need FileContentResult. I've already answered on similar question [link](http://stackoverflow.com/questions/27874360/download-an-excel-file-in-jquery-ajax-request-from-asp-net-mvc/30246687#30246687). Only difference for you is changing MediaTypeNames. – SouXin Oct 05 '15 at 15:01
  • @SouXin You're presuming MVC. This may very well be Web Forms, where `FileContentResult` isn't relevant. – mason Oct 05 '15 at 15:04
  • `data:application/pdf` style URLs are not universally supported by all browser/PDF viewer plugin combinations. – mkl Oct 05 '15 at 16:28
  • 1
    I converted bytes to a base64 string and then changed to window.open("data:application/pdf;base64, " + data); in the javascript and it worked. I will edit the question with the answer i found later. – Franco Fittipaldi Oct 05 '15 at 18:14

0 Answers0