Jump to content

Lisp to join/combine pdf files


sadhu

Recommended Posts

I have several A4 pages in pdf format printed from autocad. Since these pages are printed via lisp I have their local addresses in lisp variables.

(setq page1 "\\pdf\page1.pdf" etc.)

 

I then use "PDF Merge Tool" to combine these pages manually to create a single pdf file - instruction manual.

 

Is it possible to combine pdf file via Lisp ?

All help is appreciated.

Link to comment
Share on other sites

Spaj : These pdf files are printed within a lisp routine that changes certain values, freezes /unfreezes layers, adds lines etc according to user input. The dwg is only in model space. So I don't think I can use publish.

Link to comment
Share on other sites

I take it your Pdf merge tool is a external program if so you can use "Shell" which opens a operating system window that you can send commands to.

 

Something like ("shell" "notepad")

Link to comment
Share on other sites

  • 1 year later...

Hi,

This might be of help to someone.

 

Mearging pdf files can done quite easily with Ghostscript.

read here

 

Roy_043 posted this :

(defun KGA_String_Join (strLst delim)   
(if strLst (apply  'strcat (cons (car strLst)         
(mapcar '(lambda (a) (strcat delim a)) (cdr strLst)))) ""   ) ) 


; (CombinePdf  
;   "C:\\Program Files\\gs\\gs8.61\\bin\\gswin32c.exe" 
;   '("D:\\Tmp\\A.pdf" "D:\\Tmp\\B.pdf") ;   "D:\\Tmp\\Total.pdf" ; ) 
; Note: Existing trgFile will be overwritten. 

(defun CombinePdf (gsExe srcFileLst trgFile)
(startapp     (strcat       gsExe " "       "-dBATCH -dNOPAUSE -dQUIET -sDEVICE=pdfwrite "      
                                         "-sOutputFile=\"" trgFile "\" "      
                                        "\"" (KGA_String_Join srcFileLst "\" \"") "\""     )   ) )

You need to install Ghostcript - it's a freeware.

Edited by sadhu
Link to comment
Share on other sites

A further version thanks to roy_043

 

;Plots layouts by range
; By Alan H Feb 2014
(defun AH:pltlays ( / pdfname lay numlay numend)
(SETVAR "PDMODE" 0)
(setvar "fillmode" 1)
(setvar "textfill" 1)
(setq plotnames '())
(if (not AH:getval2) (load "getvals"))
(ah:getval2 "Enter start tab number" 6 4 "Enter end tab number" 6 4)
(setq numlay (ATOI val1))
(setq numend (ATOI val2))
(setq len (+ (- numend numlay) 1))
(setq dwgname (GETVAR "dwgname"))
(setq lendwg (strlen dwgname))
(setq dwgname (substr dwgname 1 (- lendwg 4)))
(repeat len
(vlax-for lay (vla-get-Layouts (vla-get-activedocument (vlax-get-acad-object)))
(if (= numlay (vla-get-taborder lay))
 (setvar "ctab" (vla-get-name lay))
) ; if
(setq pdfname (strcat (getvar "dwgprefix") "pdf\\" dwgname "-" (getvar "ctab") ".pdf" ))
) ; for
(setq lay nil)
(setvar "textfill" 1)
(setvar "fillmode" 1)
   (COMMAND "-PLOT"  "Y"  "" "dwg to Pdf"
       "Iso full bleed A3 (420.00 x 297.00 MM)" "m" "LANDSCAPE"  "N"   "W"  "-6,-6" "807,560" "1=2"  "C"
       "y" "Designlasercolour.ctb" "Y" "n" "n" "n" pdfName "N" "y"
   )

(setq numlay (+ numlay 1))
(setq plotnames (cons pdfname plotnames))
) ; end repeat
(setq trgfile (strcat (getvar "dwgprefix") "pdf\\" dwgname "-D" val1 "-D" val2 ".pdf")) 
) ; defun
(AH:pltlays)
(if (not combinepdf)(load "mergepdfs"))
(combinepdf gsExe plotnames trgFile )

(setq plotnames nil
          val1 nil
          val2 nil)
(princ)

 

;MergePdfs
;Merges multiple pdf (or eps) files into one
;
; make a batch file ?
;gs -sDEVICE=pdfwrite \
;    -dNOPAUSE -dBATCH -dSAFER \
;    -sOutputFile=combined.pdf \
;    first.pdf \
;    second.pdf \
;    third.pdf [...]
;Ghostscript ([url]http://www.ghostscript.com/[/url]) can be used to combine PDFs.
; Something like this should work: by Roy_043
(defun KGA_String_Join (strLst delim)
(if strLst
(apply
'strcat
(cons
(car strLst)
(mapcar '(lambda (a) (strcat delim a)) (cdr strLst))
)
)
""
)
)

(setq gsexe "C:\\Program Files\\gs\\gs9.19\\bin\\gswin64c.exe")

; Note: Existing trgFile will be overwritten.
(defun CombinePdf (gsExe srcFileLst trgFile)
(startapp 
(strcat
gsExe " "
"-sDEVICE=pdfwrite -dBATCH -dNOPAUSE -dQUIET  "
"-sOutputFile=\"" trgFile "\" "
"\"" (KGA_String_Join srcFileLst "\" \"") "\""
)
)
)

GETVALS.lsp

  • Like 1
Link to comment
Share on other sites

  • 5 years later...

Its a 5 year old post, you need to mention that you need to download Ghostscript and check where you save it to 

 

eg (setq gsexe "D:\\gs\\gs9.55.0\\bin\\gswin64c.exe")

Link to comment
Share on other sites

  • 6 months later...

Did you get anywhere on the deleting of the single separate pdfs?  I also need this.  My only thought is to have it dump the single pages in a tmp folder.

Link to comment
Share on other sites

just a thought (in case you don't have ghostscript or unable to install it) first attach each separate pdf to a dwg (script / lisp) and then publish all dwgs to a single pdf.

Link to comment
Share on other sites

If you look at my code the list of file names is made here (setq plotnames (cons pdfname plotnames)) and the files are in a directory \PDF under the current dwg location, there is VL-delete-file . So could do a Foreach loop and delete files.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...