简体   繁体   English

使用 GhostScript 打印 PDF

[英]Print PDF using GhostScript

I am in need of your support on the following issue since its pulling me for a while.我需要您对以下问题的支持,因为它拉了我一段时间。 We have a small c# utility, which print given PDF using GhostScript .我们有一个小的c#实用程序,它使用GhostScript打印给定的PDF This print as expected but fail to retain the page formatting's.此打印按预期进行,但未能保留页面格式。 However, pages are printed as expected when I switch Adobe Acrobat in place of GhostScript .但是,当我切换Adobe Acrobat代替GhostScript时,页面会按预期打印。 So I presume, I am making some obvious mistake on the GhostScript's command line arguments .所以我认为,我在 GhostScript 的命令行参数上犯了一些明显的错误。

Background背景

Following is the core c# logic, which print a given PDF file with varying style across each pages.以下是核心 c# 逻辑,它在每个页面上打印具有不同样式的给定 PDF 文件。 The given PDF file has pages;给定的 PDF 文件有页面;

  1. with inconsistent font style and colour字体样式和颜色不一致
  2. some of the pages have normal font size where others are printed in extra small一些页面具有正常的字体大小,而其他页面则以超小字体打印
  3. some of the pages has recommended margin but others have very small margin一些页面有推荐的边距,但其他页面的边距很小
  4. some of the pages are in colour and the rest in grey.一些页面是彩色的,其余的页面是灰色的。
  5. some of the pages are landscape in style where other are portrait一些页面是横向的,而其他页面是纵向的

In concise, the PDF which I am trying to print is nothing but a consolidation (joining individual pdfs into one large pdf) of numerous small sized pdf document with varying fonts style, size, margins.简而言之,我尝试打印的 PDF 只不过是许多具有不同字体样式、大小、边距的小型 pdf 文档的合并(将单个 pdf 合并为一个大 pdf)。

Issue问题

Following logic use GhostScript(v9.02) to print PDF file.以下逻辑使用GhostScript(v9.02)打印 PDF 文件。 Though the following logic print any given PDF, it fail to retain the page formatting including header, footer, font size, margin, orientation ( my pdf file has pages those both landscape and portrait).尽管以下逻辑打印任何给定的 PDF,但它无法保留页面格式,包括页眉、页脚、字体大小、边距、方向(我的 pdf 文件有横向和纵向的页面)。

Interestingly, if I use acrobat reader to print the same PDF then it will print as expected along with all page level formatting's.有趣的是,如果我使用 acrobat 阅读器打印相同的 PDF,那么它将按预期与所有页面级格式一起打印。

PDF specimen: First section , Second section PDF样本:第一节第二节

  void PrintDocument()
    {
         var psInfo = new ProcessStartInfo();
                psInfo.Arguments =
                    String.Format(
                        " -dPrinted -dBATCH -dNOPAUSE -dNOSAFER -q -dNumCopies=1 -sDEVICE=ljet4 -sOutputFile=\"\\\\spool\\{0}\" \"{1}\"",
                        GetDefaultPrinter(), @"C:\PDFOutput\test.pdf");
                psInfo.FileName = @"C:\Program Files\gs\gs9.10\bin\gswin64c.exe";
                psInfo.UseShellExecute = false;

        using (var process= Process.Start(psInfo))
        {
            process.WaitForExit();
        }
    }

I think you asked this question before, and its also quite clear from your code sample that you are using GSView , not Ghostscript .我认为您之前问过这个问题,并且从您的代码示例中也很清楚您使用的是GSView ,而不是Ghostscript

Now, while GSView does use Ghostscript to do the heavy lifting, its a concern that you are unable to differentiate between these two applications.现在,虽然 GSView 确实使用 Ghostscript 来完成繁重的工作,但您担心无法区分这两个应用程序。

You still haven't provided an example PDF file to look at, nor a command line, though you have now at least managed to quote the Ghostscript version.您仍然没有提供一个示例 PDF 文件来查看,也没有提供命令行,尽管您现在至少设法引用了 Ghostscript 版本。 You need to also give a command line (no I'm not prepared to assemble it from reading your code) and you should try this from the command line, not inside your own application, in order to show that its not your application making the error.您还需要提供一个命令行(不,我不准备通过阅读您的代码来组装它)并且您应该从命令行尝试此操作,而不是在您自己的应用程序中,以表明它不是您的应用程序错误。

You should consider upgrading Ghostscript to the current version.您应该考虑将 Ghostscript 升级到当前版本。

Note that a quick perusal of your code indicates that you are specifying a number of command line options (eg -dPDFSETTINGS) which are only appropriate for converting a file into PDF, not for any other purpose (such as printing).请注意,快速阅读您的代码表明您指定了许多命令行选项(例如 -dPDFSETTINGS),这些选项仅适用于将文件转换为 PDF,不适用于任何其他目的(例如打印)。

So as I said before, provide a specimen file to reproduce the problem, and a command line (preferably a Ghostscript command line) which causes the problem.所以正如我之前所说,提供一个样本文件来重现问题,以及导致问题的命令行(最好是Ghostscript命令行)。 Knowing which printer you are using would probably be useful too, although its highly unlikely I will have a duplicate to test on.知道您使用的是哪台打印机也可能很有用,尽管我极不可能有副本进行测试。

Answer - UPDATE 16/12/2013答案 - 更新 16/12/2013

I was managed to get it fixed and wanted to enclose the working solution if it help others.我设法修复了它,并希望附上工作解决方案,如果它可以帮助其他人。 Special thanks to 'KenS' since he spent lot of time to guide me.特别感谢'KenS',因为他花了很多时间来指导我。

To summarize, I finally decided to use GSView along with GhostScript to print PDF to bypass Adobe.总而言之,我最终决定使用 GSView 和 GhostScript 来打印 PDF 以绕过 Adob​​e。 The core logic is given below;核心逻辑如下;

 //PrintParamter is a custom data structure to capture file related info
private void PrintDocument(PrintParamter fs, string printerName = null)
        {
            if (!File.Exists(fs.FullyQualifiedName)) return;

            var filename = fs.FullyQualifiedName ?? string.Empty;
            printerName = printerName ?? GetDefaultPrinter(); //get your printer here

            var processArgs = string.Format("-dAutoRotatePages=/All -dNOPAUSE -dBATCH -sPAPERSIZE=a4 -dFIXEDMEDIA -dPDFFitPage -dEmbedAllFonts=true -dSubsetFonts=true -dPDFSETTINGS=/prepress -dNOPLATFONTS -sFONTPATH=\"C:\\Program Files\\gs\\gs9.10\\fonts\" -noquery -dNumCopies=1 -all -colour -printer \"{0}\" \"{1}\"", printerName, filename);
            try
            {

                var gsProcessInfo = new ProcessStartInfo
                                        {
                                            WindowStyle = ProcessWindowStyle.Hidden,
                                            FileName = gsViewEXEInstallationLocation,
                                            Arguments = processArgs
                                        };
                using (var gsProcess = Process.Start(gsProcessInfo))
                {

                    gsProcess.WaitForExit();

                }

        }

You could use GSPRINT .您可以使用GSPRINT

I've managed to make it work by only copying gsprint.exe/gswin64c.exe/gsdll64.dll in a directory and launch it from there.我设法通过仅将 gsprint.exe/gswin64c.exe/gsdll64.dll 复制到目录中并从那里启动它来使其工作。

sample code :示例代码:

    // This uses gsprint (mind the paths)
    private const string gsPrintExecutable = @"C:\gs\gsprint.exe";
    private const string gsExecutable = @"C:\gs\gswin64c.exe";

    string pdfPath = @"C:\myShinyPDF.PDF"
    string printerName = "MY PRINTER";


    string processArgs = string.Format("-ghostscript \"{0}\" -copies=1 -all -printer \"{1}\" \"{2}\"", gsExecutable, printerName, pdfPath );

            var gsProcessInfo = new ProcessStartInfo
                                    {
                                        WindowStyle = ProcessWindowStyle.Hidden,
                                        FileName = gsPrintExecutable ,
                                        Arguments = processArgs
                                    };
            using (var gsProcess = Process.Start(gsProcessInfo))
            {

                gsProcess.WaitForExit();

            }

Try the following command within Process.Start() :Process.Start()尝试以下命令:

gswin32c.exe -sDEVICE=mswinpr2 -dBATCH -dNOPAUSE -dNOPROMPT -dNoCancel -dPDFFitPage -sOutputFile="%printer%\\[printer_servername]\[printername]" "[filepath_to_pdf]"

It should look like this in C#:在 C# 中它应该是这样的:

string strCmdText = "gswin32c.exe -sDEVICE=mswinpr2 -dBATCH -dNOPAUSE -dNOPROMPT -dNoCancel -dPDFFitPage -sOutputFile=\"%printer%\\\\[printer_servername]\\[printername]\" \"[filepath_to_pdf]\"";
System.Diagnostics.Process.Start("CMD.exe", strCmdText);

This will place the specified PDF file into the print queue.这会将指定的 PDF 文件放入打印队列。

Note - your gswin32c.exe must be in the same directory as your C# program.注意- 您的 gswin32c.exe 必须与您的 C# 程序位于同一目录中。 I haven't tested this code.我还没有测试过这段代码。

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

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