Spire.PDF is a professional PDF library applied to creating, writing, editing, handling and reading PDF files without any external dependencies. Get free and professional technical support for Spire.PDF for .NET, Java, Android, C++, Python.

Thu Apr 14, 2016 6:58 am

Hi everybody,

I'm quite new with spire xls. I'm trying to convert an excel workbook to a pdf file that send back to the client via the Response.
After downloading the file, I cannot open it neither with GoogleChrome or adobe acrobat reader but with the pdf viewer from Mozilla it works just fine.
Adobe and googleChrome say that the file is damaged/corrupted.

Below is my code :

using (MemoryStream memoryStream = new MemoryStream(pck.GetAsByteArray()))
{
Workbook workbook = new Workbook();
workbook.LoadFromStream(memoryStream);

// Set PDF template
Spire.Pdf.PdfDocument pdfDocument = new Spire.Pdf.PdfDocument();


//Convert Excel to PDF using the template above
PdfConverterSettings settings = new PdfConverterSettings();
settings.FitSheetToOnePage = FitToPageType.ScaleWithSameFactor;


pdfDocument = PdfConvertionHelper.SaveToPdf(workbook, settings);
// Save and preview PDF
using (MemoryStream savePDFStream = new MemoryStream())
{
pdfDocument.SaveToStream(savePDFStream);
Response.ContentType = "pdf";
Response.AddHeader("content-disposition", "attachment;filename=" + pdfFileName);
Response.OutputStream.Write(savePDFStream.GetBuffer(), 0, savePDFStream.GetBuffer().Length);
}
}
pck being an ExcelPackage object generated before

Thanks for your help

cisnard1
 
Posts: 4
Joined: Tue Mar 29, 2016 7:56 am

Thu Apr 14, 2016 8:19 am

Hi,

Thanks for your posting.
Please try to set ContentType ="application/pdf". We recommend you to use new method of converting excel to pdf.
Sample code:
Code: Select all
  Workbook workbook = new Workbook();
            workbook.LoadFromStream(stream);
           workbook.SaveToStream(pdfstream, FileFormat.PDF);

If the issue still exists after trying, please share your excel file for investigation. Thank you.

Best Regards,
Amy
E-iceblue support team
User avatar

amy.zhao
 
Posts: 2774
Joined: Wed Jun 27, 2012 8:50 am

Thu Apr 14, 2016 9:15 am

Hi amy,

Thanks for your quick answer.
I've tried to change to "application/pdf" but this does not fix the problem. Also one funny thing is that if I remove the settings.FitSheetToOnePage = FitToPageType.ScaleWithSameFactor line, I can open in Adobe and Chrome. But I don't see why this should corrupt the file.
Any other idea ?

cisnard1
 
Posts: 4
Joined: Tue Mar 29, 2016 7:56 am

Thu Apr 14, 2016 9:16 am

Oh sorry haven't seen the last part of your post, I'll try that right now

cisnard1
 
Posts: 4
Joined: Tue Mar 29, 2016 7:56 am

Thu Apr 14, 2016 9:47 am

I've used the workbook.SaveToStream(pdfstream, FileFormat.PDF) method and it worked just fine.

Thank you so much

Cheers,

cisnard1
 
Posts: 4
Joined: Tue Mar 29, 2016 7:56 am

Fri Apr 15, 2016 1:21 am

Hi,

Thanks for your feedback.
I am glad to hear that. Welcome to write to us again if you have further problems.

Best Regards,
Amy
E-iceblue support team
User avatar

amy.zhao
 
Posts: 2774
Joined: Wed Jun 27, 2012 8:50 am

Return to Spire.PDF