Re: Insert PDF?
Facundo Aguilera <[email protected]>
| Newsgroups | gmane.comp.graphics.ipe.general |
|---|---|
| Message-ID | <CAPYB0AC85a_9RhpYD+bgSL-9+hKV-pSKmRUc7Tp5aY2EZVe8yg@mail.gmail.com> |
On Wed, Oct 14, 2015 at 3:51 PM, Nikolaus Rath <nrath-GBHt0Lb9jDujYgHvJn/[email protected]> wrote: > Hello, > > Is there a way to insert a PDF into an IPE document (as an image)? It > seems to this should be straightforward, but I can't seem to find the > function anywhere... > > Best, > -Nikolaus > Hi! Try attached ipelet (working on ipe 7.1.8 for linux) Regards Facu _______________________________________________ Ipe-discuss mailing list Ipe-discuss-rGrgPyRx506NN8uzcEdRPYRWq/[email protected] http://lists.science.uu.nl/mailman/listinfo/ipe-discuss
epspdfipe.lua
(text/x-lua, 6.9 KB)
-----------------------------------------------------------------------
-- epspdfipe: ipelet for drawing editor Ipe to insert EPS/PDF/IPE files
-----------------------------------------------------------------------
--[[
This file is an extension of the drawing editor Ipe (ipe7.sourceforge.net)
Copyright (c) 2010 Zhengdao Wang
This file can be distributed and modified under the terms of the GNU General
Public License as published by the Free Software Foundation; either version
3, or (at your option) any later version.
This file is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
details.
You can find a copy of the GNU General Public License at
"http://www.gnu.org/copyleft/gpl.html", or write to the Free
Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
1) For multiple pages, the page to insert can be selected.
2) The content will be grouped into one 'group' object in Ipe.
3) The object will be inserted at the center of the current Ipe page.
4) For files not parseable by Ipe, fonts can be converted to outlines,
or imported as math, as literal text, or discarded.
5) It depends on availability of ghostscript and pdftoipe.
6) It has not been fully tested. Absolutely NO WARRANTY.
--]]
label = "Insert EPS/PDF/IPE"
about = [[
IInsert EPS/PDF/IPE files.
]]
local assert=_G.assert
-- cache of number of pages
local NumPages={}
-- Option for text
local TextOpt={
"As Path",
"As Math (all text into math)",
"Literal Text",
"No Text"
}
local pdftoipe_flag={"", "-math", "-literal", "-notext"}
local merge=1
local textOpt=1
-- capture output of a command: from stackoverflow
function capture(cmd, raw)
local f = assert(_G.io.popen(cmd, 'r'))
local s = assert(f:read('*a'))
f:close()
if raw then return s end
s = string.gsub(s, '^%s+', '')
s = string.gsub(s, '%s+$', '')
s = string.gsub(s, '[\n\r]+', ' ')
return s
end
-- count the number of pages in file
local function count_numpages(file)
if NumPages[file] then return NumPages[file] end
local ext=string.sub(file:lower(),-3)
if ext==".ps" then
cmd="psselect -p_1 "..file.." 2>&1 > /dev/null "..
"| awk -F] '{print substr($1,2)}'"
return tonumber(capture(cmd))
elseif ext=="pdf" then
cmd="pdfinfo "..file.." | awk '/Pages:/{print $2}'"
local numpages=tonumber(capture(cmd))
NumPages[file]=numpages
return numpages
else
return 1
end
end
-- get page number to import
local function get_number(model,numpages)
if numpages>1 then
local d = ipeui.Dialog(model.ui:win(), "Get Page Number")
d:add("label", "label", { label="Page Number (1--"..numpages..") "}, 1, 1)
d:add("num", "input", {size=3}, 1, 2)
d:add("tlabel", "label", { label="Text Option"}, 2, 1)
d:add("textOpt", "combo", TextOpt, 2, 2)
d:add("mlabel", "label", { label="merge (small integer)"}, 3, 1)
d:add("merge", "input", {size=2}, 3, 2)
d:add("ok", "button", { label="&Ok", action="accept" }, 4, 2)
d:add("cancel", "button", { label="&Cancel", action="reject" }, 4, 1)
d:set("num", "1")
d:set("textOpt", textOpt)
d:set("merge", merge)
local r = d:execute({200,200})
if r then
textOpt=tonumber(d:get("textOpt"))
merge=tonumber(d:get("merge"))
return tonumber(d:get("num"))
else
return 1
end
else
return 1
end
end
-- the real thing
function insert(model)
-- get filename
--
dir=dir or "."
filter_open = { "Supported files (*.ipe *.ps *.pdf *.eps)", "*.ipe;*.ps;*.pdf;*.eps"}
-- example
--filter_save = { "XML (*.ipe *.xml)", "*.ipe;*.xml",
-- "PDF (*.pdf)", "*.pdf",
-- "EPS (*.eps)", "*.eps" }
local file=ipeui.fileDialog(nil, "open", "Select a file" ,filter_open, dir, nil, nil)
if not file then return end
dir=string.gsub(file, "(.*/)(.*)", "%1")
local tmpps="/tmp/ipelet-eps.ps"
local tmpeps="/tmp/ipelet-eps.eps"
local tmppdf="/tmp/ipelet-eps.pdf"
local tmpipe="/tmp/ipelet-eps.ipe"
-- convert to an Ipe file if not already one
local num=1
local format= ipe.fileFormat(file)
local numpages
if format~="xml" and format ~="eps" then
local ext=string.sub(file:lower(),-3)
if ext=="pdf" or ext==".ps" then
numpages=count_numpages(file)
if numpages>1 then
num=get_number(model,numpages)
else
num=1
end
end
local ret
if ext==".ps" and numpages>1 then
assert(_G.os.execute("psselect -p"..num.." "..file.." "..tmpps))
file=tmpps
end
local cache=""
if textOpt==1 then
cache="-dNOCACHE"
end
if ext=="pdf" or textOpt==1 then
assert(_G.os.execute("gs "..
" -q "..cache.." -dNOPAUSE -sDEVICE=epswrite -dFirstPage="
..num.." -dLastPage="..num..
" -sOutputFile="..tmpeps.." -dBATCH ".. file))
else
tmpeps=file
end
assert(_G.os.execute("ps2pdf -q " .. tmpeps .. " " .. tmppdf))
assert(_G.os.execute("pdftoipe " .. pdftoipe_flag[textOpt] ..
" -merge "..merge ..
" " .. tmppdf .. " " ..
tmpipe..">/dev/null 2>/dev/null"))
file=tmpipe
end
-- load the doc
local doc = assert(ipe.Document(file))
local layout = model.doc:sheets():find("layout")
local fs = layout.framesize
-- take the page from file and insert on the current IPE
-- one group object of the page content
for i,p in doc:pages() do
local box = ipe.Rect()
for i,obj,sel,layer in p:objects() do
box:add(p:bbox(i))
end
local nx = (fs.x - box:width()) / 2
local ny = (fs.y - box:height()) / 2
local trans = ipe.Vector(nx, ny) - box:bottomLeft()
local m = ipe.Translation(trans)
for j = 1,#p do
p:transform(j, m)
end
local elements={}
for i,obj,sel,layer in p:objects() do
elements[#elements+1]=obj
end
local group = ipe.Group(elements)
model:creation("create graph", group)
break
end
-- remove tmp files
files={tmpeps, tmppdf, tmpipe, tmpps}
for i=1,4 do
if ipe.fileExists(files[i]) then
_G.os.execute("rm " .. files[i])
end
end
model.ui:explain('import complete')
end
local function set_text_opt(model)
local d = ipeui.Dialog(model.ui:win(), "Set Text Option")
d:add("tlabel", "label", { label="Text Option"}, 1, 1)
d:add("textOpt", "combo", TextOpt, 1, 2)
d:add("mlabel", "label", { label="merge (small integer)"}, 2, 1)
d:add("merge", "input", {size=2}, 2, 2)
d:add("ok", "button", { label="&Ok", action="accept" }, 3, 2)
d:add("cancel", "button", { label="&Cancel", action="reject" }, 3, 1)
d:set("textOpt", textOpt)
d:set("merge", merge)
local r = d:execute({200,200})
if r then
textOpt=tonumber(d:get("textOpt"))
merge=tonumber(d:get("merge"))
end
end
methods = {
{ label = "Insert EPS/PDF/IPE", run=insert},
{ label = "Options", run=set_text_opt},
}