简体   繁体   English

在 Web 应用程序中使用 Ghostscript(PDF 缩略图)

[英]Using Ghostscript in a Webapplication (PDF Thumbnails)

i am using the ghostscriptsharp wrapper for c# and ghostscript.我正在为 c# 和 ghostscript 使用 ghostscriptsharp 包装器。 I want to generate thumbnails out of pdf-files.我想从 pdf 文件中生成缩略图。

There are different Methods imported form the ghostscript-c-dll "gsdll32.dll".从ghostscript-c-dll“gsdll32.dll”导入了不同的方法。

 [DllImport("gsdll32.dll", EntryPoint = "gsapi_new_instance")]
 private static extern int CreateAPIInstance(out IntPtr pinstance, 
                                        IntPtr caller_handle);

 [DllImport("gsdll32.dll", EntryPoint = "gsapi_init_with_args")]
 private static extern int InitAPI(IntPtr instance, int argc, IntPtr argv);

 //...and so on

I am using the GhostscriptWrapper for generating the thumbnails in a webapplication (.net 2.0).我正在使用 GhostscriptWrapper 在 web 应用程序 (.net 2.0) 中生成缩略图。 This class uses the methods imported above.这个类使用上面导入的方法。

 protected void Page_Load(object sender, EventArgs e){
      GhostscriptWrapper.GeneratePageThumb("c:\\sample.pdf", "c:\\sample.jpg", 1, 100, 100);
 }

When i debug the Web-Application in Visual Studio 2008 by hitting key "F5" it works fine (a new instance of webserver is generated).当我通过按“F5”键在 Visual Studio 2008 中调试 Web 应用程序时,它工作正常(生成了一个新的 Web 服务器实例)。 When i create a WindowsForm Application it works too.当我创建一个 WindowsForm 应用程序时,它也能工作。 The thumbnails get generated.缩略图被生成。

When i access the application with the webbrowser directly (http://localhoast/mywebappliation/..) it doesn't work.当我直接使用 webbrowser (http://localhoast/mywebappliation/..) 访问应用程序时,它不起作用。 No thumbnails are generated.不生成缩略图。 But there is also no exception thrown.但也没有抛出异常。

I placed the gsdll32.dll in the system32-folder of windows xp.我将 gsdll32.dll 放在 windows xp 的 system32 文件夹中。 The Ghostscript Runtime is installed too. Ghostscript 运行时也已安装。 I have given full access in the IIS-Webproject (.Net 2.0).我已在 IIS-Webproject (.Net 2.0) 中授予完全访问权限。

Does anybody know why i can't access Ghostscript from my webapplication?有人知道为什么我无法从我的网络应用程序访问 Ghostscript 吗? Are there any security-issues for accessing dll-files on the IIS-Server?访问 IIS 服务器上的 dll 文件是否存在任何安全问题?

Greetings Klaus问候克劳斯

Try changing the current directory尝试更改当前目录

string workingDirectory = @"C:\tmp";
Directory.SetCurrentDirectory(workingDirectory);
GhostscriptWrapper.GeneratePageThumb("c:\\sample.pdf", "c:\\sample.jpg", 1, 100, 100);

I now have a workaround.我现在有一个解决方法。 I am not accessing Ghostscript with the GhostscriptWrapper-Class.我没有使用 GhostscriptWrapper-Class 访问 Ghostscript。 Instead i access the cmd.exe on the server directly.相反,我直接访问服务器上的 cmd.exe。 The following method takes a command (ghostscript syntax) and runs it in cmd.exe.以下方法接受一个命令(ghostscript 语法)并在 cmd.exe 中运行它。 I used the following method for doing this:我使用以下方法来做到这一点:

public static string runCommand(string workingDirectory, string command)
    { 
        // Create the ProcessInfo object
        System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo("cmd.exe");
        psi.UseShellExecute = false;
        psi.RedirectStandardOutput = true;
        psi.RedirectStandardInput = true;
        psi.RedirectStandardError = true;
        psi.WorkingDirectory = workingDirectory;

        // Start the process
        System.Diagnostics.Process proc = System.Diagnostics.Process.Start(psi);

        // Attach the output for reading
        System.IO.StreamReader sOut = proc.StandardOutput;

        // Attach the in for writing
        System.IO.StreamWriter sIn = proc.StandardInput;

        sIn.WriteLine(command);

        // strm.Close();

        // Exit CMD.EXE
        string stEchoFmt = "# {0} run successfully. Exiting";

       // sIn.WriteLine(String.Format(stEchoFmt, targetBat));
        sIn.WriteLine("EXIT");

        // Close the process
        proc.Close();

        // Read the sOut to a string.
        string results = sOut.ReadToEnd().Trim();

        // Close the io Streams;
        sIn.Close();
        sOut.Close();

        // Write out the results.
        string fmtStdOut = "<font face=courier size=0>{0}</font>";
        return String.Format(fmtStdOut, results.Replace(System.Environment.NewLine, "<br>"));
    }

可能是您运行网站的身份没有 c:\\

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM