使用ghostscript生成PDF到图像不能正常工作 [英] Generating PDF to image using ghostscript is not working properly

查看:137
本文介绍了使用ghostscript生成PDF到图像不能正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用ghostscript创建PDF到图像。它在我的本地网站看起来不错。现在我使用App Application for Default站点将我的网站设置为IIS,并使用LAN连接从其他PC访问它我的网站看起来不错,但ghostscript没有生成Pdf到Image。有什么我错过了吗?



我正在使用gsdll32.dll文件



我尝试过:



GhostscriptWrapper.GenerateOutput(sourcePdfFilePath,destinationPngFilePath,

新的GhostscriptSettings

{

Device = GhostscriptDevices.pngalpha,

Page = new GhostscriptPages

{

Start = 1,

结束= 1,

AllPages = false

},

分辨率=新尺寸

{

高度= 72,

宽度= 72

},

尺寸=新GhostscriptPageSize

{

Native = GhostscriptPageSizes.a4

}

}

);

I am trying to create PDF to image using ghostscript. It looks good in my local site. Now I set my website to the IIS using App Application for Default site, and accessing it from other PC using LAN connection my site is looks good but ghostscript is not generating Pdf to Image. Is there anything that I was missing out?

I am using gsdll32.dll file

What I have tried:

GhostscriptWrapper.GenerateOutput(sourcePdfFilePath, destinationPngFilePath,
new GhostscriptSettings
{
Device = GhostscriptDevices.pngalpha,
Page = new GhostscriptPages
{
Start = 1,
End = 1,
AllPages = false
},
Resolution = new Size
{
Height = 72,
Width = 72
},
Size = new GhostscriptPageSize
{
Native = GhostscriptPageSizes.a4
}
}
);

推荐答案

您没有发布错误消息,但我遇到的一个常见问题是ghostscript和gsdll32.dll,它需要位于已部署的projects / bin目录中(例如:c:\inetpub \ www.root \ yourprojectname\bin)。我相信gsdll32.dll不能作为参考包含在您的项目中,因此在部署应用程序时不会在发布时自动获取。
You didn't post an error message but one common issue i've had with ghostscript and the gsdll32.dll is that it needs to be located in your deployed projects /bin directory (Ex: c:\inetpub\wwwroot\yourprojectname\bin). I believe the gsdll32.dll cannot be included as a reference in your project so it will not be automatically picked up on publish when deploying your app.


我刚刚为批处理OCR做了这个应用程序,发现使用Ghostscript.NET(看到这里)工作得很好,很可靠。我发现的最大问题是因为开发站安装了Ghostscript,并不意味着系统运行例程。在这种情况下,最好提供一种使用指定的gsdll32.dll文件路径在内存中初始化Ghostscript的方法。这是我工作代码的略微修改版本。唯一的区别是我们没有提供和输入路径,我们提供了一个包含该数据的对象。



I had just done this for a batching OCR application and found that using Ghostscript.NET (see here)worked great and was reliable. The biggest issue I found was that just because the development station has Ghostscript installed, does not mean the system running the routine does. In this case, its best to provide a means to initialize Ghostscript in memory using a specified gsdll32.dll file path. This is slightly modified version of my working code. The only difference is that we do not provide and input path, we provide an object that has that data in it.

using Ghostscript.NET;

static GhostscriptVersionInfo GetVersionInfo(string dllPath)
        {
            GhostscriptVersionInfo gsVersion = null;

            try { gsVersion = GhostscriptVersionInfo.GetLastInstalledVersion(GhostscriptLicense.GPL | GhostscriptLicense.AFPL, GhostscriptLicense.GPL); }
            catch { }

            if (gsVersion == null)
            {
                try { gsVersion = new GhostscriptVersionInfo(new Version(0, 0, 0), dllPath, string.Empty, GhostscriptLicense.GPL); }
                catch { }
            }

            return gsVersion;
        }


        static void ExtractPagesAsPngs(GhostscriptVersionInfo gsVersion, string scrFilePath, string destFilePath, int[] sheetsToExtract, int[] allowedDpiValues)
        {
            if ((allowedDpiValues == null) || (allowedDpiValues.Length < 1)) { allowedDpiValues = new int[] {300, 256, 150, 128, 96, 72, 64, 32, 16, 8}; }
            
            // Set up the GhostscriptPngDevice object.
            var gsDev = new GhostscriptPngDevice(GhostscriptPngDeviceType.Png256);
            gsDev.GraphicsAlphaBits = GhostscriptImageDeviceAlphaBits.V_4;
            gsDev.TextAlphaBits = GhostscriptImageDeviceAlphaBits.V_4;
            gsDev.CustomSwitches.Add("-dDOINTERPOLATE");
            
            // Iterate thought the sheets to extract, and extract them.
            for (var i = 0; i < sheetsToExtract.Length; i++)
            {
                // Specify the PDF and sheet number.
                gsDev.InputFiles.Add(scrFilePath);
                gsDev.Pdf.FirstPage = sheetsToExtract[i];
                gsDev.Pdf.LastPage = sheetsToExtract[i];

                // Set the output file path (if multiple sheets, place sheet number in parenthsies.
                if (sheetsToExtract.Length == 1) { gsDev.OutputPath = destFilePath; }
                else { gsDev.OutputPath = destFilePath.Replace(".png", " (" + sheetsToExtract[i].ToString() + ").png"); }

                // Prevent zero (0) or negative DPI values.
                for (var j = 0; j < allowedDpiValues.Length; j++)
                {
                    if (allowedDpiValues[j] < 1) { allowedDpiValues[j] = 1; }
                }

                // Remove duplicate allowed DPI values.
                var dpiToTry = allowedDpiValues.Distinct().OrderBy(x => x).ToArray();

                // Attempt to extect the image at highest allowed DPI, and decrementing by using the next approved DPI
                // specified until successfull or no more allowed DPI values exsist.
                int dpiIdx = 0;
                bool extracted = false;
                while (!extracted && (dpiIdx < dpiToTry.Length))
                {
                    // Set the resolution.
                    gsDev.ResolutionXY = new GhostscriptImageDeviceResolution(dpiToTry[dpiIdx], dpiToTry[dpiIdx]);

                    // Try to extract the image.
                    try { gsDev.Process(gsVersion, true, null); }
                    catch { }

                    // Check if successful.
                    if (File.Exists(gsDev.OutputPath)) { extracted = true; }
                    else { dpiIdx++; }
                }
            }
        }


这篇关于使用ghostscript生成PDF到图像不能正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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