在MVC C#环境中将文档转换为pdf [英] Converting a doc to a pdf in a mvc c# environment

查看:128
本文介绍了在MVC C#环境中将文档转换为pdf的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个在特定用户(app.pool用户)下运行的c#MVC应用程序. 我需要将doc或docx文件转换为pdf.

Hi I've got a c# MVC application running under a particular user (app.pool user). I need to convert a doc or docx file to a pdf.

我认为一个不错的选择是使用libreoffice触发将启动此过程的进程.

I thought a good option would be to use libreoffice to fire a process that would start this.

为了使自己的生活更轻松(如果libreoffice无法正常工作),我使用了一个批处理文件.

To make life easier for myself (and if libreoffice shouldn't work) I used a batch file.

echo on

SET var1=%2

IF "%var1:~-1%"=="\" SET var1=%var1:~0,-1%

cd %var1%

echo %1

echo %var1%

start /wait "" "C:\Program Files (x86)\LibreOffice 4\program\soffice" -headless -convert-to pdf %1 -outdir %var1%

我的启动代码如下.

var ba = @"C:\inetpub\wwwroot\apps\xxxxxxxxx\Services\convert.bat";
                fullPath = @"C:\inetpub\wwwroot\apps\xxxxxxxxx\Files\Temp\636295920370843147.doc";
                var tempPath = @"C:\inetpub\wwwroot\apps\xxxxxxxxx\Files\Temp";

                string command = ba;

                //Process.Start(command, fullPath + " " + tempPath);

                var processInfo = new ProcessStartInfo("cmd.exe", "/c " + command + " "+ fullPath+ " "+ tempPath);
                processInfo.CreateNoWindow = false;
                processInfo.UseShellExecute = false;
                processInfo.RedirectStandardError = true;
                processInfo.RedirectStandardOutput = true;

                var process = Process.Start(processInfo);

                process.OutputDataReceived += (object sender, DataReceivedEventArgs e) =>
                    Trace.WriteLine("output>>" + e.Data);
                process.BeginOutputReadLine();

                process.ErrorDataReceived += (object sender, DataReceivedEventArgs e) =>
                    Trace.WriteLine("error>>" + e.Data);
                process.BeginErrorReadLine();

                process.WaitForExit();

                Trace.WriteLine("ExitCode: {0}", process.ExitCode.ToString());
                process.Close();

这似乎确实可以手动运行,但是当我运行代码时,我可以看到整个过程停滞在转换过程中.查看线程,似乎加载了gdiplus.dll,我认为这是一个图形模块,看起来似乎不正确.

This does seem to work manually but when I run the code I can see the whole thing just stalls around the conversion. Looking at the threads and it seems to load the gdiplus.dll which I think is a graphics module which doesn't seem right.

但是,当我将其复制并粘贴到Windows中的运行"框中时,该过程运行正常,并且生成了pdf.

The process though when I copy and paste that into my 'run' box in windows works fine and the pdf is made.

我检查了app.pool用户是否可以访问libreoffice以及包含文件的文件夹. 我以app.pool用户的身份运行它,在C#中它停在process.WaitForExit();上 线.直到我终止该过程.

I've checked that the app.pool user has access to both libreoffice and also the folder with the files. I've ran this as the app.pool user, in the c# it just stalls on the process.WaitForExit(); line. Until I kill the process.

有什么想法吗?

我还尝试了许多不同的方式来执行办公室转换.刚好从C#开始-libreoffice 4,5.

I've also tried many different ways of executing the soffice conversion. just straight from the c# - libreoffice 4,5.

我已经看到有些人将libreoffice用作服务,这是一个选择吗?如果可以,怎么办? 理查德

I've seen some people use libreoffice as a service, is this an option? If so how? Richard

编辑 嗯,只是将应用程序池用户更改为我自己,现在可以使用,因此标准应用程序池用户具有权限.如何找出什么......

EDIT Ah, just changed the app Pool user to myself and now it works, so there is a permissions thing with the standard app pool user. How to find out what....

推荐答案

我将用户添加到计算机上的Administrators组中,并重新启动了计算机,然后计算机正常工作.

I added the user into the administrators group on the computer and restarted the machine, then it worked.

这可能不是最好的解决方案,但是经过2天的尝试,我不再争论了.

It might not be the best solution but after 2 days of trying to get this to work I'm not going to argue.

理查德

这篇关于在MVC C#环境中将文档转换为pdf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆