Reputation: 684
I am using Nreco PDF to convert html pages to PDF . Recently Bundling and Minification was implemented and hence the compilation debug = false
was set in the web config .
Since then the PDF Generation is failing and chrome shows up this message saying "Failed to Load PDF Document".
When i turn on debug mode , everything works as expected.
Here is the code snippet:
public ActionResult ABC()
{
var htmlContent =System.IO.File.ReadAllText(Server.MapPath("~/Views/abc/abc.html"));
createpdf(htmlContent);
}
public void createpdf(string htmlContent)
{
var htmlToPdf = new NReco.PdfGenerator.HtmlToPdfConverter();
var pdfBytes = htmlToPdf.GeneratePdf(htmlContent);
Response.ContentType = "application/pdf";
Response.ContentEncoding = System.Text.Encoding.UTF8;
Response.AddHeader("Content-Disposition", "Inline; filename=TEST.pdf");
Response.BinaryWrite(pdfBytes);
Response.Flush();
Response.End();
}
I would like to know whats causing this code to fail when run with debug =false.
Upvotes: 0
Views: 1364
Reputation: 4902
Not necessary an answer:
Decorate code with a try -> catch, debug an see if any errors in catch, most likely htmlToPdf.GeneratePdf will fail, if not continue with next debugging steps
Make sure your PDF generator is working correctly, meaning you will have a valid pdf file, before returning the bytes store pdf on you App_Data folder in solution
var appDataPath = Server.MapPath("~/App_Data");
var filename = string.Format(@"{0}.pdf", DateTime.Now.Ticks);
var path = Path.Combine(appDataPath, filename);
System.IO.File.WriteAllBytes(path, pdfBytes.ToArray());
Check creating and closing response
Response.Clear();
Response.ClearContent();
Response.ClearHeaders();
Response.ContentType = "application/pdf";
Response.ContentEncoding = System.Text.Encoding.UTF8;
Response.AddHeader("Content-Disposition", "Inline; filename=TEST.pdf");
Response.BinaryWrite(pdfBytes);
Response.Flush();
Response.Close()
Response.End()
Edit: After long debugging sessions, what was discovered: When using WebMarkupMin.Mvc for compress content or minimization Controller Action result is compressed incorrect (is correct, but not with your way of return the pdf).
webMarkupMin default settings are as following:
enableMinification="true"
disableMinificationInDebugMode="false"
enableCompression="true"
disableCompressionInDebugMode="false"
This is the reason why in debug mode was running correct when
compilation debug="true"
To get same error when debug="true" in web.config set:
<webMarkupMin xmlns="http://tempuri.org/WebMarkupMin.Configuration.xsd">
<webExtensions enableMinification="true" disableMinificationInDebugMode="false" enableCompression="true" disableCompressionInDebugMode="false" />
<!-- rest of shebang -->
</webMarkupMin>
Where is the issue you might ask, well is simple in your implmentation Response stream is compressed incorrect.
Overcoming the problem:
//you can use FileResult, same outcome
public ActionResult ABC()
{
var htmlContent = System.IO.File.ReadAllText(Server.MapPath("~/Views/abc/abc.html"));
var htmlToPdf = new NReco.PdfGenerator.HtmlToPdfConverter();
var pdfBytes = htmlToPdf.GeneratePdf(htmlContent);
//return File(pdfBytes, "application/pdf", "TEST.pdf");
//will add filename to your response, downside is that browser downloads the response
//if your really need Content Disposition in response headers uncomment below line
//Response.AddHeader("Content-Disposition", "Inline; filename=TEST.pdf");
return File(pdfBytes, "application/pdf");
}
Upvotes: 2
Reputation: 370
change to use File() to return the bytes, like :
return File(pdfBytes, "application/pdf");
do not use the Response.BinaryWrite() directly return the bytes, this will cause the minifer can not read the pdf stream, so return a empty response to client
Upvotes: 0