简体   繁体   English

使用gswin32c从PS通过Shell获得PDF

[英]PDF from PS through Shell with gswin32c

I am trying to produce "c:\\output.pdf" from existed and well formed "output.ps" file with VB.NET and shell to gswin32c.exe which is in current directory. 我正在尝试使用VB.NET和外壳将存在且格式良好的“ output.ps”文件与现有目录中的gswin32c.exe生成“ c:\\ output.pdf”。

But I obviously can't write shell command properly: 但是我显然不能正确编写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

From cmd window those command regularly produce "output.pdf" 这些命令从cmd窗口定期生成“ output.pdf”

What is incorrect and how to get it working? 什么是不正确的以及如何使其正常工作?

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)

Actually the command string doesn't need quotation marks, I tested it without them. 实际上,命令字符串不需要引号,我在没有引号的情况下对其进行了测试。 You must use 8.3 file naming convention though. 但是,您必须使用8.3文件命名约定 Note that in this code the input and output filenames do not start or end with quotation marks; 请注意,在此代码中,输入和输出文件名不以引号开头或结尾; That is why you must use 8.3 file naming convention for this to succeed. 这就是为什么必须使用8.3文件命名约定才能成功的原因。 And no spaces in the file names or paths. 并且文件名或路径中没有空格。

Your problem is that it can't find the file; 您的问题是它找不到文件。 relying on the currently active directory is not a good practice as it can cause problems. 依靠当前活动目录不是一个好习惯,因为它可能会引起问题。 The solution is to provide full path and file name with no spaces and using 8.3 file naming convention for both path and file name. 解决方案是提供不带空格的完整路径和文件名,并对路径和文件名使用8.3文件命名约定。

Also make sure that GSDLL32.DLL is in the same folder as GSWin32C.exe. 此外,请确保GSDLL32.DLL与GSWin32C.exe位于同一文件夹中。

I did some more testing and found that by using quotation marks in the command string, it takes long file names just fine. 我进行了更多测试,发现通过在命令字符串中使用引号,只需很长的文件名即可。

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

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

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