I’m happy to announce the little PDFmerger.jl package to merge PDF (Portable Document Format) files. It is tested on Linux, OS X, and Windows and does not require (manual) installation of external tools.
Besides simple installation, it provides a convenient way to produce a PDF file containing multiple plots on different pages. This topic came up before and there is an open issue for Plots.jl.
using Plots
using PDFmerger: append_pdf!
for t in 1:5
p = plot(rand(33), title="$t")
savefig(p, "temp.pdf")
append_pdf!("allplots.pdf", "temp.pdf", cleanup=true)
end
This produces allplots.pdf that contains all five plots and deletes the temporary file. (Has anybody an idea how to avoid to manually specify a temporary file name? A clever macro maybe?)
I hope that may be helpful! I’m open to any suggestions
That could be very helpful, thanks !
A solution without macro:
function with_merge_pdfs(inner, output, iterables...)
empty!(output) # to replace with rm(output_path) in final version
map(iterables...) do args...
mktemp() do path, io
inner(output, path, args...)
end
end
output
end
As an example involving only arrays, no file (just for testing)
let
output = [] # would be replaced with output = "output.pdf"
with_merge_pdfs(output, 1:3) do outp, temp, x
push!(outp, (temp, x)) # would be replaced with the plot commands
end
output
end
ouputs
The final version would be used as
with_merge_pdfs("allplots.pdf", 1:5) do all, temp, t
p = plot(rand(33), title="$t")
savefig(p, temp)
append_pdf!(all, temp)
end