Ghostscript via lisp

Discussion in 'AutoCAD' started by steedj, Apr 1, 2004.

  1. steedj

    steedj Guest

    Is there anyway to run Ghostscript via Lisp. I can easily create a postscript plot using a lisp routine but is there anyway to program that same lisp routine to use Ghostscript to Automatically make a PDF?

    Thank You!
     
    steedj, Apr 1, 2004
    #1
  2. Why don't you just invest in a low cost PDF printer driver, and plot directly to
    PDF without going through EPS ? Create a PC3 file for PDF creation, and write a
    wrapper VLisp macro that plots using that PC3.

    Matt

     
    Matt Stachoni, Apr 1, 2004
    #2
  3. steedj

    Aubrie Gibbs Guest

    It would seem quicker and easier to just use one of the free or low cost
    front ends to ghostscript such as CutePDF but if you want more control, then
    just use the startapp function from lisp. The only issue I found was
    needing to set the "current" directory to where GS is installed so it can
    find the initialization file. You can use the dos_chdir doslib function for
    this. I'm not familiar with all of the GS options, so this may not be
    necessary. Try something along this line...

    ;EPS2PDF
    ;Converts postscript file to pdf using ghostscript
    ;Requires DOSLib from Robert McNeel & Associates www.mcneel.com
    ;
    (defun EPS2PDF ( EPSFILE PDFFILE / GSLOC GSEXE CURDIR GSARGS )

    (setq GSLOC "C:\\gs\\bin\\"
    GSEXE "gswin32c.exe"
    CURDIR (dos_pwdir)
    )

    ;Make sure filenames are surrounded by double quotes
    (if (/= (substr EPSFILE 1 1) "\"")
    (setq EPSFILE (strcat "\"" EPSFILE "\""))
    )
    (if (/= (substr PDFFILE 1 1) "\"")
    (setq EPSFILE (strcat "\"" PDFFILE "\""))
    )

    (setq GSARGS (strcat
    " -sDEVICE=pdfwrite"
    " -q"
    " -dPDFSETTINGS=/prepress"
    " -dCompatibilityLevel=1.3"
    " -dNOPAUSE"
    " -dBATCH"
    " -sOutputFile=" PDFFILE
    " -c save pop"
    " -f " EPSFILE
    ))

    (dos_chdir GSLOC)
    (startapp GSEXE GSARGS)
    (dos_chdir CURDIR)

    (princ)
    )

    Make sure to change the file locations/names to match your system.

    Aubrie
     
    Aubrie Gibbs, Apr 2, 2004
    #3
  4. steedj

    steedj Guest

    Thank You Aubrie!
    That's exactly what I needed.
    I was previously trying to utilize the STARTAPP lisp command but I simply did not know how to set it up to run the Ghostscript functions -- nor did I have DosLib.

    Thank you so much!

    Man, this will allow me to not only make single PDF's in a flash but with a little more tweaking and a lot more code , I'll be able to make multi-sheet PDF's by issuing a single command.

    There was one slight little typo error in your code as follows:

    (setq EPSFILE (strcat "\"" PDFFILE "\""))

    Should be: (setq PDFFILE (strcat "\"" PDFFILE "\""))

    In the above, the variable EPSFILE was changed to PDFILE.

    You probably did what I do a lot -- you copy/pasted from another line and then forgot to change the variable name.
    Easy mistake to make.

    I appreciate your help so very very much.

    Thank You sir, you have definitely put me on the right track!
    J. Steed
     
    steedj, Apr 2, 2004
    #4
  5. steedj

    steedj Guest

    Is there anyway to do multi-sheet PDF that way?
    I've used the CutePDFwriter driver to make a single sheet PDF but how do I create multi-sheet PDF's using a driver like that?

    Thank you for your reply Matt -- I appreciate it,
    J.Steed
     
    steedj, Apr 2, 2004
    #5
  6. steedj

    Aubrie Gibbs Guest

    I threw it together quickly out of curiosity but did not thoroughly test
    it...oh well.
    You might add a check for the existence of the pdf file prior to creating it
    since the current version overwrites existing files.

    I'd like to see what you come up with if you don't mind sharing....

    Thanks
    Aubrie
     
    Aubrie Gibbs, Apr 2, 2004
    #6
  7. Matt Stachoni, Apr 2, 2004
    #7
  8. steedj

    Aubrie Gibbs Guest

    After looking at some documentation for GhostScript, I removed the need
    for DosLib by using the "-I" option. The merge issue appears to just
    involve adding the list of files (pdf or eps) at the end. I decided to use
    the "@" option with an arguments file to work around any potential string
    length issues........

    ;EPS2PDF
    ;Converts postscript file to pdf using GhostScript
    ;
    (defun EPS2PDF ( EPSFILE PDFFILE / GSLOC GSEXE GSARGS )

    (setq GSLOC "C:\\gs\\bin\\"
    GSEXE "gswin32c.exe"
    )

    ;Make sure filenames are surrounded by double quotes
    (if (/= (substr EPSFILE 1 1) "\"")
    (setq EPSFILE (strcat "\"" EPSFILE "\""))
    )
    (if (/= (substr PDFFILE 1 1) "\"")
    (setq PDFFILE (strcat "\"" PDFFILE "\""))
    )

    (setq GSARGS (strcat
    " -i" "\"" GSLOC "\""
    " -sDEVICE=pdfwrite"
    " -q"
    " -dPDFSETTINGS=/prepress"
    " -dCompatibilityLevel=1.3"
    " -dNOPAUSE"
    " -dBATCH"
    " -sOutputFile=" PDFFILE
    " -c save pop"
    " -f " EPSFILE
    ))

    (startapp GSEXE GSARGS)

    (princ)
    )


    ;MergePdfs
    ;Merges multiple pdf (or eps) files into one
    ;
    (defun MergePdfs ( PDFFILE FILELST / GSLOC GSEXE
    TMPNAM FILEID FNAME )

    (setq GSLOC "C:\\gs\\bin\\"
    GSEXE "gswin32c.exe"
    TMPNAM "MergePdf.txt"
    TMPNAM (strcat (vl-filename-directory PDFFILE) "\\" TMPNAM)
    )

    ;Make sure filename is surrounded by double quotes
    (if (/= (substr PDFFILE 1 1) "\"")
    (setq PDFFILE (strcat "\"" PDFFILE "\"") )
    )

    ;Create temp file containing arguments for GhostScript to process
    (setq FILEID (open TMPNAM "w") )
    (write-line (strcat "-I" "\"" GSLOC "\"") FILEID)
    (write-line "-sDEVICE=pdfwrite" FILEID)
    (write-line "-q" FILEID)
    (write-line "-dPDFSETTINGS=/prepress" FILEID)
    (write-line "-dCompatibilityLevel=1.3" FILEID)
    (write-line "-dNOPAUSE" FILEID)
    (write-line "-dBATCH" FILEID)
    (write-line (strcat "-sOutputFile=" PDFFILE) FILEID)
    (write-line "-c save pop" FILEID)
    (write-line "-f " FILEID)
    ;Write out each filename passed in to merge
    (foreach FNAME FILELST
    ;Make sure filename is surrounded by double quotes
    (if (/= (substr FNAME 1 1) "\"")
    (setq FNAME (strcat "\"" FNAME "\""))
    )
    (write-line FNAME FILEID)
    )
    (close FILEID)

    ;Run GhostScript referencing temp file
    (startapp (strcat GSLOC GSEXE) (strcat "@\"" TMPNAM "\"") )

    (princ)
    )

    Aubrie
     
    Aubrie Gibbs, Apr 5, 2004
    #8
  9. steedj

    steedj Guest

    My hat's off to you sir -- you definitely steered me in the right direction. Many thanks!
    As far as adding a pre-existing PDF checker, that's a great idea; however, I really do not need that function at the present time. The way we do our plots, overwriting the existing PDF is essential.

    Further edits of your code enabled me to eliminate the DosLIB functions. This for me is a good thing since my company is so strict on using outside, unapproved (by them) software. The way I have it now, both my dwg and PDF end up in the same folder location.

    Anyhow, as far a sharing what I have -- sure, I can do that.
    So far, I have a working routine that allows me to create a single sheet pdf from a Postscript utilizing GhostScript.

    To run this routine, I first open the target drawing and then load and run the routine. Furthermore, the Postscript creation portion of my code contains a reference to the postscript Printer we use (HP750/PS) and it also pulls up my custom Plotstyle (PDF_Plotstyle.ctb). Of course, you will have to change the Printer and plotstyle in the code to your settings; Furthermore, that whole ' (Command "-plot" ' line of code will have to be edited to suit your's or whoever's settings.

    BTW, Speaking of a Postscript driver -- does anyone know of a Postscript Driver that will plot to ANSI sizes (i.e. 11x17, 17x22, 22x34) that will allow for a 1-to-1 plot? With the driver I use, I must plot everything to Fit. (I may post this question by itself if I get no replies).


    Here's the code I have so far:

    ;;PS2PDF -- Create PDF from Postscript.

    (defun C:pS2PDF (/ dwg_name pltname DwgPfx plotFILE EPSFILE PDFFILE GSLOC GSEXE GSARGS)

    ;; dwg_name -- Get drawing name
    ;; pltname -- Seperate name from extension
    ;; DwgPfx -- Retrive drawing prefix -- open file's location
    ;; plotFILE -- Merge DwgPfix pltname
    ;; PDFFILE -- Merge plotFILE with pdf extension
    ;; EPSFILE -- Merge plotFILE with ps extension:

    (setq dwg_name (getvar "dwgname")
    pltname (substr dwg_name 1 (- (strlen dwg_name) 4))
    DwgPfx (getvar "dwgprefix")
    plotFILE (strcat DwgPfx pltname)
    PDFFILE (strcat DwgPfx pltname ".pdf")
    EPSFILE (strcat DwgPfx pltname ".plt")
    );setq
    ;;Run Plot command:
    (command "-plot" "Y" "Layout1" "HP DesignJet 750C/PS" "11x17" "I" "L" "N" "E" "F" "C" "Y" "PDF_PlotStyle.ctb" "Y" "N" "N" "N" "Y" plotFILE "N" "Y"
    )
    ;;define Ghostscript location & set variable to Ghostscript exe file:
    (setq GSLOC "C:\\ACAD2000\\CADmanager\\PLOTTING\\GhostScrpit\\gs7.04\\bin\\"
    GSEXE "gswin32c.exe"
    );setq

    ;; set Ghostscript switches:
    (setq GSARGS (strcat
    " -sDEVICE=pdfwrite"
    " -q"
    " -dPDFSETTINGS=/prepress"
    " -dCompatibilityLevel=1.3"
    " -dNOPAUSE"
    " -dBATCH"
    " -sOutputFile=" PDFFILE
    " -c save pop"
    " -f " EPSFILE
    ));setq
    ;; Run Ghostscript:
    (startapp GSEXE GSARGS)
    (princ)
    );end PS2PDF

    Next, I'm going to write additional code in an attempt to create a multipage PDF from a Single Drawing with that has multi-sheets contained within (i.e. sheet 1 of 3, sheet 2 of 3, etc.).
    The challenging part will be getting the sheets in the correct order.

    Thanks again,
    J. Steed
     
    steedj, Apr 5, 2004
    #9
  10. I just d'loaded the freeware CutePDF Writer (ghostscript writer). If you
    plot to a file, it is an .EPS file.


    I really do not need that function at the present time. The way we do our
    plots, overwriting the existing PDF is essential.
    This for me is a good thing since my company is so strict on using outside,
    unapproved (by them) software. The way I have it now, both my dwg and PDF
    end up in the same folder location.
    pdf from a Postscript utilizing GhostScript.
    the routine. Furthermore, the Postscript creation portion of my code
    contains a reference to the postscript Printer we use (HP750/PS) and it also
    pulls up my custom Plotstyle (PDF_Plotstyle.ctb). Of course, you will have
    to change the Printer and plotstyle in the code to your settings;
    Furthermore, that whole ' (Command "-plot" ' line of code will have to be
    edited to suit your's or whoever's settings.
    Driver that will plot to ANSI sizes (i.e. 11x17, 17x22, 22x34) that will
    allow for a 1-to-1 plot? With the driver I use, I must plot everything to
    Fit. (I may post this question by itself if I get no replies).
    "N" "E" "F" "C" "Y" "PDF_PlotStyle.ctb" "Y" "N" "N" "N" "Y"
    plotFILE "N" "Y"
    multipage PDF from a Single Drawing with that has multi-sheets contained
    within (i.e. sheet 1 of 3, sheet 2 of 3, etc.).
     
    Craig Vaughn AIA CSI, Apr 5, 2004
    #10
  11. We have found and used successfully another freeware PDF generator, called
    pdf995 (do a search). It simply appears on your list of available printers,
    and you print to it from any Windows program that prints/plots (CAD, word
    processing, spreadsheet, photo manipulation, database, e-mail, web browser,
    etc., etc.), only instead of putting it on paper, it asks for a PDF file
    name and makes that out of whatever you want to print/plot. You could
    "plot" a drawing to multiple PDF pages by plotting windows of portions of
    it, which I supposed might be automatable somehow.
    It also has related freeware programs available that will combine multiple
    PDF's into one, extract the text content from PDF's, and some other neat
    things.

    Kent Cooper, AIA
     
    Kent Cooper, AIA, Apr 5, 2004
    #11
  12. steedj

    Aubrie Gibbs Guest

    After looking at some documentation (see my other post), I found that
    the standard install adds environment variables on the system so that gs
    knows where to find its files. In my case, I'm just using the minimum
    version provided by cutepdf so I perfer to use the -I option.

    I'll have to look into your ps driver issue...

    Aubrie
     
    Aubrie Gibbs, Apr 6, 2004
    #12
  13. steedj

    Aubrie Gibbs Guest

    As it happens, these low priced utilities are really just convenient
    front ends to the ghostscript program. They simplify the process for
    typical user needs but do not offer a programmatic interface that I am aware
    of. It appears that ghostscript also offers a dll based api that could be
    used from VB/A in addition to the command line interface. I can see some
    value in trying to streamline and automate the creation of multi-page pdfs.
    Now setting up a sub sheet set in 2005 for an rfi or other submittal that
    involves multiple drawings and be able to background batch plot then all
    into a single pdf, that would be sweet! Oh and email and archive it as
    well! I digress...

    Aubrie
     
    Aubrie Gibbs, Apr 6, 2004
    #13
  14. Hi Aubrie,

    Why do you want to spend ages programming, dumb your data down, increase the
    file size by a huge amount and reduce the displayable precision with a PDF
    file ?

    Why not use a DWF file ?

    --


    Laurie Comerford
    CADApps
    www.cadapps.com.au
     
    Laurie Comerford, Apr 6, 2004
    #14
  15. steedj

    Aubrie Gibbs Guest

    Well I can't say it's by choice. PDF is just more prevalent and
    understood. The precision is sufficient on screen as well as print and the
    "dumb your data down" aspect is part of the point for some people. As for
    spending ages programming, I'm not, this was just a quick look at
    ghostscript which I've been intending to do for a while...
    I realize what I mentioned can be done in 2005 with dwf but IMHO pdfs are
    not going away anytime soon, so for now one must juggle both...

    Aubrie
     
    Aubrie Gibbs, Apr 6, 2004
    #15
  16. steedj

    steedj Guest

    Yep Craig, CutePDF writer does plot to file as a Postscript; however it does not have a "D" Ansi sheet selection.

    I just discovered an HP PostScript Driver, available at www.hp.com, that has all A-thru-D Ansi sheet sizes available: HP DesignJet 800PS3 24
    I can now successfully plot at 1-to-1 for all my required Ansi sheet sizes.

    Also, I guess I could have done this to get my desired sheet sizes, (this was suggested by a friend):
    1.) Go to Control Panel -> Printers.
    2.) Choose Server Properties command on File pulldown menu.
    3.) Add desired paper sizes on Forms tab.

    Thanks for the Reply.
     
    steedj, Apr 6, 2004
    #16
  17. My version has "Arch A" through "Arch E" and "Arch E1" built in. I just
    filtered out the rest and that was all I needed. But, I didn't see the Ansi
    "D"...



    does not have a "D" Ansi sheet selection.
    has all A-thru-D Ansi sheet sizes available: HP DesignJet 800PS3 24
     
    Craig Vaughn AIA CSI, Apr 6, 2004
    #17
Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.