--*************************************************************************
-- Software: PDF_PREVIEW
-- Version: 0.2 - D11.5+ VERSION
--
-- Requirements/Dependencies:
-- - Shell Xtra
-- - Win: pdftoppm.exe, ppmtobmp.exe, libnetpbm10.dll (in same folder as Shell Xtra)
-- - Mac: PNG Import Export.xtra, Mix Services.xtra
--
-- Creates PDF Preview as lingo image object
--
--**************************************************************************
property ancestor
property _tmpdir
property _tmpmem
----------------------------------------
-- CONSTRUCTOR
----------------------------------------
on new (me, pdfObj)
if voidP(pdfObj) then pdfObj=script("PDF").new()
me.ancestor=pdfObj
if the platform contains "win" then
me._tmpdir = shell_cmd("echo %TMP%", ["eol":EMPTY]) & "\"
else
me._tmpdir = "/tmp/"
end if
me._tmpmem = new(#bitmap, castlib("tmp"))
return me
end
----------------------------------------
-- arguments pageNum and dpi only supported for windows
----------------------------------------
on Preview (me, pageNum, dpi)
if voidP(pageNum) then pageNum=1
if voidP(dpi) then dpi = 150
pdfFile = me. _tmpdir & "~preview.pdf"
if the platform contains "win" then -- WIN
-- save as tmp-file
me.Output(pdfFile)
shell_setCurrentDir("") -- folder of shell xtra
-- escape path, if necessary
if pdfFile contains SPACE then pdfFile = QUOTE & pdfFile & QUOTE
-- render PDF to PPM file (called ".-000001.ppm" etc.)
cmd = "pdftoppm.exe -q -f "&pageNum&" -l "&pageNum&" -r "&dpi&" "&pdfFile&" %TMP%\ 2>nul"
shell_cmd(cmd)
-- convert PPM to BMP file
len = string(pageNum).length
ppmFile = "-000000".char[1..(7-len)]&pageNum&".ppm"
cmd = "ppmtobmp.exe ""E&me._tmpdir&ppmFile"E&">%TMP%\~preview.bmp 2>nul"
shell_cmd(cmd)
-- load ~preview.bmp
me._tmpmem.importFileInto(me._tmpdir&"~preview.bmp")
img = me._tmpmem.image.duplicate()
-- clean up
me._tmpmem.image = image(0,0,32)
shell_cmd("del "&pdfFile)
shell_cmd("del %TMP%\"&ppmFile)
shell_cmd("del %TMP%\~preview.bmp")
else -- MAC
-- save as tmp-file
me.Output(shell_posix2hfs(pdfFile))
-- render PDF to PNG file
cmd = "sips -s format png "&pdfFile&" --out /tmp/~preview.png"
shell_cmd(cmd)
-- load ~preview.png
fn = shell_posix2hfs("/tmp/~preview.png")
me._tmpmem.importFileInto(fn)
img = me._tmpmem.image.duplicate()
-- clean up
me._tmpmem.image = image(0,0,32)
shell_cmd("rm "&pdfFile)
shell_cmd("rm /tmp/~preview.png")
end if
return img
end