Click here to Skip to main content
15,895,256 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dears i'm a web developer using MVC i want to convert files i have already uploaded it works fine on my local pc but when i publish solution on server it not working and not throw any errors..

this is my code

What I have tried:

C#
public jsonResult Convertfiletopdf(string filename , string filePatgh)

    {
        try
        {

            filePatgh = Path.Combine(Server.MapPath("~/Uploads/"));
            if (filename.Contains(".xlsx"))
            {
                var newfilename = filename.Replace(".xlsx", ".pdf");
                var app = new Application();
                var wkb = app.Workbooks.Open(filePatgh + filename);
                if (!System.IO.File.Exists(filePatgh + newfilename))
                {                      
                    wkb.ExportAsFixedFormat(
                        XlFixedFormatType.xlTypePDF,
                        filePatgh + newfilename,
                        XlFixedFormatQuality.xlQualityStandard,
                        true, true, 1, 10, false);

                }
                wkb.Close();

            }
            if (filename.Contains(".xls"))
            {
                var newfilename = filename.Replace(".xls", ".pdf");
                var app = new Application();
                var wkb = app.Workbooks.Open(filePatgh + filename);

                if (!System.IO.File.Exists(filePatgh + newfilename))
                {
                    wkb.ExportAsFixedFormat(
                        XlFixedFormatType.xlTypePDF,
                        filePatgh + newfilename,
                        XlFixedFormatQuality.xlQualityStandard,
                        true,
                        true,
                        1,
                        10,
                        false);

                    // wkb.ExportAsFixedFormat(XlFixedFormatType.xlTypePDF, filePatgh + newfilename);
                }
                wkb.Close();
            }
            else
            {

                var objWorPdf = new Word2Pdf();
                var fromLocation = filePatgh + "\\" + filename;
                var fileExtension = Path.GetExtension(filename);

                var changeExtension = filename.Replace(fileExtension, ".pdf");
                object toLocation = filePatgh + "\\" + changeExtension;
                objWorPdf.InputLocation = fromLocation;
                objWorPdf.OutputLocation = toLocation;
                if (!System.IO.File.Exists(filePatgh + changeExtension))
                {
                    objWorPdf.Word2PdfCOnversion();
                }



            }
            return Json(filename, JsonRequestBehavior.AllowGet);
        }
        catch (Exception ex)
        {
            throw ;
        }
    }
Posted
Updated 8-Aug-16 21:30pm
v2
Comments
NathanRO 8-Aug-16 10:51am    
Have you verified that Microsoft Excel and Word are installed on the server?
Manas Bhardwaj 8-Aug-16 12:04pm    
No. That's not a good idea. MS Office is a client application.
Eman2020 9-Aug-16 3:55am    
yes it installed
Richard Deeming 8-Aug-16 11:24am    
You should read the following Microsoft knowledgebase article:
Considerations for server-side Automation of Office[^]
Microsoft does not currently recommend, and does not support, Automation of Microsoft Office applications from any unattended, non-interactive client application or component (including ASP, ASP.NET, DCOM, and NT Services), because Office may exhibit unstable behavior and/or deadlock when Office is run in this environment.

In addition to installing Office on the server, there are several hoops you'll have to jump through to stand any chance of getting your code to work. And even if you do get it to work, there's no guarantee that it won't stop working at some random point in the future.
Member 12444785 9-Aug-16 5:36am    

1 solution

As Richard stated correctly, MS Office is not usable here. But there are some more options, some free, some with a price tag:
- OpenOffice / LibreOffice
"Path\soffice.exe" --convert-to pdf "Path\Input.docx" --outdir "Path\Output folder"
- TotalDocConverter
- PdfConvert
Note that many converters promise to work on the server, but fail. And depending on the input documents, the pdf files may look a little different from your input files.
I suggest you try OpenOffice/LibreOffice first, and only if you can't live with those minor problems, try other programs.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900