We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

Created PDFs are corrupted and will not open

Converting a regular ASP.NET project to MVC. I have a business layer method that produces a byte array that is saved as a PDF. This method works fine in the ASP.NET project but produces a corrupted PDF in the MVC project. Even the simples HTML doesn't produce a good PDF. A sample HTML string:

<!DOCTYPE html><html lang=\"en\" xmlns=\"http://www.w3.org/1999/xhtml\"><body>Hello world</body></html>

The relevant code:

            HtmlToPdfConverter htmlConverter = new HtmlToPdfConverter(HtmlRenderingEngine.WebKit);
            WebKitConverterSettings settings = new WebKitConverterSettings();
            PdfMargins margins = new PdfMargins();
            margins.All = 50;
            settings.Margin = margins;
            settings.WebKitPath = @"C:\Program Files (x86)\Syncfusion\WebKitHTMLConverter\16.1.0.24\QtBinaries";
            htmlConverter.ConverterSettings = settings;
            PdfDocument pdfDoc = htmlConverter.Convert(HTMLString, "");
            MemoryStream stream = new MemoryStream();
            pdfDoc.Save(stream);
            return stream.ToArray();

The good PDF produced by the ASP.NET project for the above HTML is 7K. The MVC PDF is 11K. :\

This has become a time sensitive issue. Any help would be appreciated.

5 Replies

KK Karthik Krishnaraj Syncfusion Team December 7, 2018 10:28 AM UTC

Hi Jim,  
  
Thank you contacting Syncfusion support.   
  
We have tried to reproduce the reported issue and it is working fine with ASP.NET MVC project in our end. So, can you please save the output PDF document to a file directly instead of returning in a byte array.   
  
Refer this code snippet for saving the pdf document directly to file.   
PdfDocument pdfDoc = htmlConverter.Convert(HTMLString, "");  
              
MemoryStream stream = new MemoryStream();  
  
string outputPath = Server.MapPath("/Content/") +  "OutputPdf.pdf";  
pdfDoc.Save(outputPath);  
  
pdfDoc.Save(stream);  
              
return stream.ToArray();  
  
Kindly check the saved PDF file (OutputPdf.pdf) is corrupted or not. So that we can confirm, whether the issue is occurs in HTML to PDF conversion or when saving the byte array to PDF file. If the saved file is corrupted, kindly share us the document to analyze further on this. If the document is not corrupted, please check the byte array to PDF file conversion.   
  
Please find the sample from the following link. 
   
Sample: 
Regards, 
Karthik.  



JP Jim Perry December 7, 2018 01:03 PM UTC

The client part of the code handles saving the PDF:

Response.AddHeader("content-disposition", "attachment;filename=Questionnaire.pdf");
return File(data, "application/pdf");

Attached is the PDF created in the MVC project.

Attachment: questionnaire_66aac8cd.zip


KK Karthik Krishnaraj Syncfusion Team December 10, 2018 01:15 PM UTC

Hi Jim,  
 
We have tried to reproduce the reported issue in our end with the provided details and it is working fine. We have attached the modified sample based on the code snippet provided in the last update. Please find the sample below,  
 
Sample: 
 
Can you please try the below steps and provide the requested details,   
1.     Environment details such as OS, bit version, culture settings etc., 
2.     Save the input HTML to a file in the machine where the conversion take place and check the contents are properly displayed in web browsers (Safari or chrome). 
3.     Save the output PDF document to a file in the controller using the below code snippet and share us the document. 
PdfDocument pdfDoc = htmlConverter.Convert(HTMLString, "");            
MemoryStream stream = new MemoryStream();    
    
string outputPath = Server.MapPath("/Content/") +  "OutputPdf.pdf";    
pdfDoc.Save(outputPath);    
    
pdfDoc.Save(stream);    
return stream.ToArray();    
  
4.     We can direclty Save/Open PDF document to the client from controller using below code snippet, below try the below code snippet and check the output PDF document from the client. 
PdfDocument pdfDoc = htmlConverter.Convert(HTMLString, "");  
pdfDoc.Save("Sample.pdf", HttpContext.ApplicationInstance.Response, HttpReadType.Save);  


 
5.     If still you are getting the same exception, please use the custom assembly which will generates logs and temporary files inside the QtBinaries folder. Kindly share us the log files and temporary file to us. It will helpful for us to analyze and assist you further on this.   
  
Custom Assembly: 
Note: Please take backup of Syncufsion.HtmlConverter.Base.dll in your application and replace that assembly with the above custom assembly. After generating logs and files, you can replace this assembly with Syncfusion.HtmlConverter.Base.dll.   
 
Kindly try the above steps and provide us the details, so that we can analyze further to reproduce the issue. If still we will not able to reproduce the issue then we can have web meeting to directly look into the issue and provide you the solution at the earliest.  
 
Regards, 
Karthik. 



JP Jim Perry December 11, 2018 08:14 PM UTC

The suggestions here would mean changing how the business layer and client work. The PDF related code is in the business layer as multiple projects use it.

The MVC project has an AJAX call which calls a controller action which calls the business layer. The business layer uses the PDF library to return a byte array which is saved to a PDF. The same byte array saves correctly in a regular ASP.NET project, but produces a corrupted PDF in the MVC project.


DB Dilli Babu Nandha Gopal Syncfusion Team December 18, 2018 09:16 AM UTC

Hi Jim,   
  
Thanks for your patience.   
  
We have analyzed further to save the document to the client side using Ajax call and created sample to achieve your requirement. There are two ways to return the file from the controller to the client side using Ajax, please find the details and samples below.    
  1. Return the output PDF document as base64 string from controller and save it as a PDF file in client side. Please find the sample for this from below link. 
  
Sample:  
Please refer the following code snippets for return the PDF document as base64 string.    
   
Controller:   
public string GeneratePDF()   
{   
    byte[] data = HTMLtoPDF();   
    return Convert.ToBase64String(data);   
}   
           
public byte[] HTMLtoPDF()   
{   
    string htmlString = "<html><body>Hello world</body></html>";   
    string baseURL = string.Empty;   
   
    //Initialize HTML to PDF converter    
    HtmlToPdfConverter htmlConverter = newHtmlToPdfConverter(HtmlRenderingEngine.WebKit);   
    WebKitConverterSettings settings = new WebKitConverterSettings();   
   
    //Set WebKit path   
    settings.WebKitPath = Server.MapPath("/QtBinaries/");   
    //Assign WebKit settings to HTML converter   
    htmlConverter.ConverterSettings = settings;   
   
    //Convert URL to PDF   
    PdfDocument document = htmlConverter.Convert(htmlString, baseURL);   
   
    MemoryStream stream = new MemoryStream();   
   
    //Save and close the PDF document    
    document.Save(stream);   
    document.Close(true);   
    return stream.ToArray();   
}   
   
   
Ajax:   
    <script type="text/javascript">   
    $('#btn1').click(function () {   
   
        $.ajax({   
            url: '/Home/GeneratePDF',   
            type: "POST",   
            success: function (data) {   
                var a = document.createElement('a');   
                var pdfAsDataUri = "data:application/pdf;base64," + data;   
                a.download = 'export.pdf';   
                a.type = 'application/pdf';   
                a.rel='nofollow' href = pdfAsDataUri;   
                a.click();   
            }   
        });   
    });   
    </script>   
   
Please let us know if you have any further queries. 
 
Regards,   
Dilli babu. 


Loader.
Live Chat Icon For mobile
Up arrow icon