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?