【问题标题】:PDF from PS through Shell with gswin32c使用 gswin32c 从 PS 到 Shell 的 PDF
【发布时间】:2013-09-03 21:37:41
【问题描述】:

我正在尝试使用 VB.NET 和 shell 将现有且格式良好的“output.ps”文件生成“c:\output.pdf”到当前目录中的 gswin32c.exe。

但我显然无法正确编写shell命令:

If LCase(p_printer).Contains("ghostscript") Then

    ' to not show old one
    IO.File.Delete(OutputPDF)
    If IO.File.Exists(InputPS) Then
        Dim commandString As String = """gswin32c.exe -sDEVICE=pdfwrite -dNOPAUSE -dBATCH -dSAFER -dQUIET -sOUTPUTFILE=" & OutputPDF & " " & InputPS & """"
        Debug.Print(commandString)

        Shell(commandString, AppWinStyle.NormalFocus)
        If IO.File.Exists(OutputPDF) And bln_showpdf Then
            'show PDF
            Start(OutputPDF)
        End If
    Else
        MsgBox(InputPS + " do NOT exists.", MsgBoxStyle.Critical)
    End If
End If

在 cmd 窗口中,这些命令会定期生成“output.pdf”

什么是不正确的以及如何使它工作?

【问题讨论】:

  • 请不要更改您原来的问题,因为这会影响我的回答。你应该恢复原来的问题。然后在那个放下面:编辑:把你的新代码放在这里......这样我们就可以看到原始问题加上你的新代码。这样一来,一切都变得更加清晰。
  • 好的,我会记住这一点,现在,不幸的是我永久删除了旧的命令字符串(因为它不起作用)。对不起。
  • 没问题;查看我修改后的答案。我对其进行了测试,效果很好。

标签: vb.net ghostscriptsharp


【解决方案1】:
Dim InputPS as String = "C:\Temp\output.ps" 'must use 8.3 file naming convention
Dim OutputPDF as String = "C:\Temp\output.pdf" 'must use 8.3 file naming convention
Dim CommandString as String = "C:\GS\gswin32c.exe -sDEVICE=pdfwrite -dNOPAUSE -dBATCH -dSAFER -dQUIET -sOUTPUTFILE=" & OutputPDF & " " & InputPS

Debug.Print(CommandString)
Shell(CommandString, AppWinStyle.NormalFocus)

其实命令字符串是不需要引号的,我在没有引号的情况下测试过。你必须使用8.3 file naming convention。请注意,在此代码中,输入和输出文件名不以引号开头或结尾;这就是为什么您必须使用 8.3 文件命名约定才能成功。并且文件名或路径中没有空格。

你的问题是找不到文件;依赖当前的活动目录不是一个好习惯,因为它可能会导致问题。解决方案是提供不带空格的完整路径和文件名,并为路径和文件名使用 8.3 文件命名约定。

还要确保 GSDLL32.DLL 与 GSWin32C.exe 位于同一文件夹中。

【讨论】:

  • 嗨 D_Bester,现在我的 print.debug 给出:“gswin32c.exe -sDEVICE=pdfwrite -dNOPAUSE -dBATCH -dSAFER -dQUIET -sOUTPUTFILE=c:\output.pdf c:\output.ps "(带引号)和 shell 命令产生错误“找不到文件”。确定我也可以访问 exe 文件和 InputPS 在哪里。我给出了更好的代码和平,看看我试图得到什么以及如何得到结果。
  • 更新的代码也不起作用。 gsdll32.dll 存在于当前目录中。问题是命令窗口(带有 ghostscript 徽标图标)闪烁太快,我看不到那里的文字。它是 3 行,首先是文本:“%%[ ProductName: GPL Ghostscript ]%%”。原始路径包含空格“C:\Program Files (x86)\gs\gs8.64\bin”这就是我在当前路径中使用副本的原因。
  • 我现在用gsdll32.dll、gsdll.lib、gswin32c.exe、gswin32.exe制作了C:\GS,使用绝对路径也不行。
  • 您也可以尝试使用“开始”>“运行”>“CMD”来检查错误。在命令窗口中,右键单击标题并单击编辑 > 粘贴您的命令字符串。
  • 这是命令字符串的 Debug.Print:C:\GS\gswin32c.exe -sDEVICE=pdfwrite -dNOPAUSE -dBATCH -dSAFER -dQUIET -sOUTPUTFILE=C:\output.pdf C:\output .ps,从 CMD 窗口可以正常工作,使用 cmets 生成 output.pdf:%%[ ProductName: GPL Ghostscript ]%%, %%[Page: 1]%%, %%[LastPage]%%。
【解决方案2】:

我做了一些更多的测试,发现通过在命令字符串中使用引号,它需要长文件名就可以了。

Public Function ConvertToPDF(ByVal svPsFileName, ByVal svPDFName)

    'check for file
    If Not IO.File.Exists(svPsFileName) Then
        Throw New ApplicationException(svPsFileName & " cannot be found")
    End If

    'check for file
    If IO.File.Exists(svPDFName) Then
        Throw New ApplicationException(svPDFName & " already exists")
    End If

    'convert
    Dim myProcInfo As New ProcessStartInfo
    myProcInfo.FileName = "C:\Program Files\GhostScript\GSWIN32C.EXE"
    myProcInfo.Arguments = "-sDEVICE=pdfwrite -q -dSAFER -dNOPAUSE -sOUTPUTFILE=""" & svPDFName & """ -dBATCH """ & svPsFileName & """"
    Debug.Print(myProcInfo.Arguments)

    'do the conversion
    Dim myProc As Process = Process.Start(myProcInfo)

    'wait for finish (no more than 20 seconds)
    myProc.WaitForExit(20000)

    'delete PS
    If IO.File.Exists(svPDFName) Then IO.File.Delete(svPsFileName)

End Function

【讨论】:

  • 这项工作很好,最好用空格覆盖路径名,因为它们经常在 Windows 中使用...
猜你喜欢
  • 1970-01-01
  • 2011-03-23
  • 1970-01-01
  • 2013-10-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-09-23
  • 1970-01-01
相关资源
最近更新 更多