1

I'm trying to render a pdf file created with IronPDF in an iframe, but it only shows a blank file. But if I save the file to the server drive, it works like a charm.

Could it be an encoding problem? This is my code:

$.ajax({
    url: $("#host").val() + 'api/Utils/PrintFile',
    method: "POST",
    responseType: "arraybuffer"
}).then(function (response) {
    var file = new Blob([response], { type: 'application/pdf' });
    var fileURL = URL.createObjectURL(file);

    $("#iPDF").attr('src', fileURL);
    $("#iPDF").height("500px");
});


[HttpPost]
[Route("PrintFile")]
public FileStreamResult PrintFile()
{
    var Renderer = new IronPdf.HtmlToPdf();
    var print = Renderer.RenderUrlAsPdf("http://MyTestUrl/");
    //print.SaveAs("C:\\Test\\MyFile.pdf");   <-- This works fine!
    var objPrint = print.Stream;

    HttpContext.Response.Headers.Add("Content-Type", "application/pdf");
    HttpContext.Response.Headers.Add("Content-Disposition", String.Format("{0}; filename=Print.pdf; size={1}", "attachment", objPrint.Length.ToString()));

    return File(objPrint, "application/pdf;");
}

Thanks in advance!

Cpt. Awesome
  • 87
  • 1
  • 2
  • 13
  • As you're using an iframe, you could set the source directly to the action and remove the ajax (and remove `[httppost]`) – freedomn-m Jun 03 '20 at 11:13

0 Answers0