开发者

Using Ghostscript in a Webapplication (PDF Thumbnails)

i am using the ghostscriptsharp wrapper for c# and ghostscript. I want to generate thumbnails out of pdf-files.

There are different Methods imported form the ghost开发者_Go百科script-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). 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). When i create a WindowsForm Application it works too. The thumbnails get generated.

When i access the application with the webbrowser directly (http://localhoast/mywebappliation/..) it doesn't work. No thumbnails are generated. But there is also no exception thrown.

I placed the gsdll32.dll in the system32-folder of windows xp. The Ghostscript Runtime is installed too. I have given full access in the IIS-Webproject (.Net 2.0).

Does anybody know why i can't access Ghostscript from my webapplication? Are there any security-issues for accessing dll-files on the IIS-Server?

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. Instead i access the cmd.exe on the server directly. The following method takes a command (ghostscript syntax) and runs it in 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>"));
    }


Its possible that the identity you are running the web site under does not have write permissions for c:\

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜