I am generating a Pdf using ItextSharp using html to pdf. I want to display a preview of the Pdf on the screen so the user can see it. The user will be able to play around with the html and get the desired resulting pdf. I am generating the pdf correctly but am having trouble displaying it on the screen.
Ajax call:
function Preview()
{
var HtmlInfo = document.getElementById('HtmlToBarCodes').value;
$.ajax({
url: '/BarCodeGenerator/PreviewPDF',
type: 'POST',
cache: false,
data: { Html: HtmlInfo },
success: function (data) {
Loading = false;
$("#pdfResult").html(
$('<iframe>', {
src: data,
width: '600px',
height: "800px"
}));
},
error: function (x) {
Loading = false;
}
});
}
Controller
[HttpPost]
[ValidateInput(false)]
public FilePathResult PreviewPDF(string Html)
{
FileStream fs = null;
string pdfFilePath = "";
try
{
//ItextSharp, Html -> Pdf
pdfFilePath = BLL.GenerateBarCodes.GenerateBarcodes(Html, 5);
Response.ClearHeaders();
Response.ClearContent();
Response.Buffer = true;
Response.AppendHeader("Content-Disposition", "inline; attachment; filename=Preview.pdf");
Response.ContentType = "application/pdf";
Response.WriteFile(pdfFilePath);
Response.Flush();
Response.End();
//fs = new FileStream(pdfFilePath, FileMode.Open, FileAccess.Read);
}
catch (Exception ex)
{
//ToDo: log error
}
return File(pdfFilePath, "application/pdf", "Preview.pdf");
}
I can confirm that the Pdf is generating and saving on my end in my C:Temp folder and I can view it as well, but when I click preview all i get is a blank. I am not sure how to load the pdf data from the ajax call or if it is even possible.