1

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.

But I obviously can't write shell command properly:

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"

What is incorrect and how to get it working?

Wine Too
  • 4,515
  • 22
  • 83
  • 137
  • Please don't change your original question as it affects my answer. You should restore the orginal question as it was. Then below that put: EDIT: put your new code here... That way we can see the original question plus your new code. Everything stays clearer that way. – D_Bester Sep 04 '13 at 08:33
  • OK, I would keep that in mind, For now, unfortunately I deleted old command string permanently (since it is not working). Sorry. – Wine Too Sep 04 '13 at 08:54
  • No problem; check out my revised answer. I tested it and it works great. – D_Bester Sep 04 '13 at 08:56

2 Answers2

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)

Actually the command string doesn't need quotation marks, I tested it without them. You must use 8.3 file naming convention though. 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. 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.

Also make sure that GSDLL32.DLL is in the same folder as GSWin32C.exe.

D_Bester
  • 5,723
  • 5
  • 35
  • 77
  • Hi D_Bester, now my print.debug gives that: "gswin32c.exe -sDEVICE=pdfwrite -dNOPAUSE -dBATCH -dSAFER -dQUIET -sOUTPUTFILE=c:\output.pdf c:\output.ps" (with quotes) and shell command produces error "File not found." where is SURE that I have accessible exe file and InputPS too. I give a better peace of code to see what and how I am trying to get result. – Wine Too Sep 04 '13 at 06:59
  • Updated code also don't work. gsdll32.dll is present in current directory. Problem is that command window (with icon of ghostscript logo) flashes too fast and i cant see what is text there. It was 3 lines and in first is that text: "%%[ ProductName: GPL Ghostscript ]%%". Original path contain spaces "C:\Program Files (x86)\gs\gs8.64\bin" thats why I use copy in current path. – Wine Too Sep 04 '13 at 09:05
  • I made now C:\GS with gsdll32.dll, gsdll.lib, gswin32c.exe, gswin32.exe, use absolute path and also don't work. – Wine Too Sep 04 '13 at 09:14
  • Also you can try checking for an error by using Start > Run > CMD. In the command window, right-click on the title and click Edit > paste your command string. – D_Bester Sep 04 '13 at 09:29
  • This is Debug.Print of command string: C:\GS\gswin32c.exe -sDEVICE=pdfwrite -dNOPAUSE -dBATCH -dSAFER -dQUIET -sOUTPUTFILE=C:\output.pdf C:\output.ps, from CMD window it work OK, produces output.pdf with comments: %%[ ProductName: GPL Ghostscript ]%%, %%[Page: 1]%%, %%[LastPage]%%. – Wine Too Sep 04 '13 at 09:49
  • Are you saying it still does not work using Shell from VB.net? – D_Bester Sep 04 '13 at 09:52
  • HUH, I have to apologize for locating a problem badly. PDF IS created with this command string (and probably will be with old one). Problem is that I have to add Threading.Thread.Sleep(300) between Shell command and Start because without that program is faster than GS and passes over If IO.File.Exists(OutputPDF). Can I tell Shell to wait untill finish? – Wine Too Sep 04 '13 at 10:06
  • I find "Wait" for shell, thank you D_Bester, now everything work as expected! – Wine Too Sep 04 '13 at 10:09
  • Here is your original Shell command: Shell("gswin32c -sDEVICE=pdfwrite -dNOPAUSE -dBATCH -dSAFER -dQUIET -sOUTPUTFILE=""c:\output.pdf"" c:\output.ps", AppWinStyle.NormalFocus). Got it by clicking on the Edited hyperlink. – D_Bester Sep 04 '13 at 11:14
  • OK, Thanks. In meantime I produce much of PDF's :) (and other image formats) with my program and your command string! – Wine Too Sep 04 '13 at 11:35
0

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
D_Bester
  • 5,723
  • 5
  • 35
  • 77
  • this work nice and it is better to cover path names with spaces as they are often used in windows... – Wine Too Sep 10 '13 at 18:23