1. --*************************************************************************
  2. -- Software: PDF_PREVIEW
  3. -- Version:  0.2 - D11.5+ VERSION
  4. --
  5. -- Requirements/Dependencies:
  6. -- - Shell Xtra
  7. -- - Win: pdftoppm.exe, ppmtobmp.exe, libnetpbm10.dll (in same folder as Shell Xtra)
  8. -- - Mac: PNG Import Export.xtra, Mix Services.xtra
  9. --
  10. -- Creates PDF Preview as lingo image object
  11. --
  12. --**************************************************************************
  13. property ancestor
  14.  
  15. property _tmpdir
  16. property _tmpmem
  17.  
  18. ----------------------------------------
  19. -- CONSTRUCTOR
  20. ----------------------------------------
  21. on new (me, pdfObj)
  22.   if voidP(pdfObj) then pdfObj=script("PDF").new()
  23.   me.ancestor=pdfObj
  24.  
  25.   if the platform contains "win" then
  26.     me._tmpdir = shell_cmd("echo %TMP%", ["eol":EMPTY]) & "\"
  27.   else
  28.     me._tmpdir = "/tmp/"
  29.   end if
  30.  
  31.   me._tmpmem = new(#bitmap, castlib("tmp"))
  32.  
  33.   return me
  34. end
  35.  
  36. ----------------------------------------
  37. -- arguments pageNum and dpi only supported for windows
  38. ----------------------------------------
  39. on Preview (me, pageNum, dpi)
  40.   if voidP(pageNum) then pageNum=1
  41.   if voidP(dpi) then dpi = 150
  42.    
  43.   pdfFile = me. _tmpdir & "~preview.pdf"
  44.  
  45.   if the platform contains "win" then -- WIN
  46.    
  47.     -- save as tmp-file
  48.     me.Output(pdfFile)
  49.    
  50.     shell_setCurrentDir("") -- folder of shell xtra
  51.    
  52.     -- escape path, if necessary
  53.     if pdfFile contains SPACE then pdfFile = QUOTE & pdfFile & QUOTE
  54.    
  55.     -- render PDF to PPM file (called ".-000001.ppm" etc.)
  56.     cmd = "pdftoppm.exe -q -f "&pageNum&" -l "&pageNum&" -r "&dpi&" "&pdfFile&" %TMP%\ 2>nul"
  57.     shell_cmd(cmd)
  58.    
  59.     -- convert PPM to BMP file
  60.     len = string(pageNum).length
  61.     ppmFile = "-000000".char[1..(7-len)]&pageNum&".ppm"
  62.     cmd = "ppmtobmp.exe "&QUOTE&me._tmpdir&ppmFile&QUOTE&">%TMP%\~preview.bmp 2>nul"
  63.     shell_cmd(cmd)
  64.    
  65.     -- load ~preview.bmp
  66.     me._tmpmem.importFileInto(me._tmpdir&"~preview.bmp")
  67.    
  68.     img = me._tmpmem.image.duplicate()
  69.    
  70.     -- clean up
  71.     me._tmpmem.image = image(0,0,32)
  72.     shell_cmd("del "&pdfFile)
  73.     shell_cmd("del %TMP%\"&ppmFile)
  74.     shell_cmd("del %TMP%\~preview.bmp")
  75.    
  76.   else -- MAC
  77.    
  78.     -- save as tmp-file
  79.     me.Output(shell_posix2hfs(pdfFile))
  80.    
  81.     -- render PDF to PNG file
  82.     cmd = "sips -s format png "&pdfFile&" --out /tmp/~preview.png"
  83.     shell_cmd(cmd)
  84.    
  85.     -- load ~preview.png
  86.     fn = shell_posix2hfs("/tmp/~preview.png")
  87.     me._tmpmem.importFileInto(fn)
  88.    
  89.     img = me._tmpmem.image.duplicate()
  90.    
  91.     -- clean up
  92.     me._tmpmem.image = image(0,0,32)
  93.     shell_cmd("rm "&pdfFile)
  94.     shell_cmd("rm /tmp/~preview.png")
  95.    
  96.   end if
  97.  
  98.   return img
  99. end
  100.  
[raw code]