# \[ANN\] PDFmerger.jl: produce multiple plots on separate pages in one PDF file

**URL:** <https://discourse.julialang.org/t/ann-pdfmerger-jl-produce-multiple-plots-on-separate-pages-in-one-pdf-file/74788>\
**Category:** Package Announcements\
**Tags:** pdf-format\
**Created:** [January 18, 2022, 8:49am UTC](https://discourse.julialang.org/t/ann-pdfmerger-jl-produce-multiple-plots-on-separate-pages-in-one-pdf-file/74788 "2022-01-18T08:49:25Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![scheidan1](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/scheidan1/32/24771_2.png) [@scheidan1](https://discourse.julialang.org/u/scheidan1)\
**Post date:** [January 18, 2022, 8:49am UTC](https://discourse.julialang.org/t/ann-pdfmerger-jl-produce-multiple-plots-on-separate-pages-in-one-pdf-file/74788/1 "2022-01-18T08:49:25Z")

</div>

Hi all,

I’m happy to announce the little [PDFmerger.jl](https://github.com/scheidan/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](https://discourse.julialang.org/t/save-multiple-plots-on-separate-pages-of-one-pdf/41953) and there is an [open issue](https://github.com/JuliaPlots/Plots.jl/issues/2908) for `Plots.jl`.

```julia
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 🙂

Best,  
Andreas

---

<div class="post-metadata">

**Author:** ![Storopoli](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/storopoli/32/209278_2.png) [@Storopoli](https://discourse.julialang.org/u/Storopoli)\
**Post date:** [January 18, 2022, 12:20pm UTC](https://discourse.julialang.org/t/ann-pdfmerger-jl-produce-multiple-plots-on-separate-pages-in-one-pdf-file/74788/2 "2022-01-18T12:20:45Z")

</div>

Does it work with `Makie.jl`?

---

<div class="post-metadata">

**Author:** ![scheidan1](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/scheidan1/32/24771_2.png) [@scheidan1](https://discourse.julialang.org/u/scheidan1)\
**Post date:** [January 18, 2022, 12:46pm UTC](https://discourse.julialang.org/t/ann-pdfmerger-jl-produce-multiple-plots-on-separate-pages-in-one-pdf-file/74788/3 "2022-01-18T12:46:22Z")

</div>

If you save the Makie figure as PDF (with CairoMakie) it should work.

PDFmerger does only handle PDF files, so it doesn’t know (or care) if the files where produced by Makie, Plots.jl, or anything else.

---

<div class="post-metadata">

**Author:** ![ederag](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ederag/32/4106_2.png) [@ederag](https://discourse.julialang.org/u/ederag)\
**Post date:** [January 18, 2022, 3:47pm UTC](https://discourse.julialang.org/t/ann-pdfmerger-jl-produce-multiple-plots-on-separate-pages-in-one-pdf-file/74788/4 "2022-01-18T15:47:27Z")

</div>

> [@scheidan1](#):
>
> 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:

```julia
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)

```julia
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  
 ![Screenshot_20220118_163126](https://global.discourse-cdn.com/julialang/original/3X/c/a/cad8f64581d23ebe399cb0a867b6fa19dbbf2dcf.png)

The final version would be used as

```julia
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

```

To be checked, as I’m still learning 🙂

---

<div class="post-metadata">

**Author:** ![lawless-m](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lawless-m/32/30869_2.png) [@lawless-m](https://discourse.julialang.org/u/lawless-m)\
**Post date:** [January 18, 2022, 4:24pm UTC](https://discourse.julialang.org/t/ann-pdfmerger-jl-produce-multiple-plots-on-separate-pages-in-one-pdf-file/74788/5 "2022-01-18T16:24:19Z")

</div>

On the [matplotlib page for savefig](https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.savefig.html) it says:

> savefig(fname, …

> Parameters
> 
> **fname** str or path-like or binary file-like
> 
> A path, or a Python file-like object.

A file-like-object is one which supports the .read() and .write() interface.

Suggesting one could put together an object which somehow uses IOBuffer rather than files.

---

<div class="post-metadata">

**Author:** ![photor](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/photor/32/14343_2.png) [@photor](https://discourse.julialang.org/u/photor)\
**Post date:** [February 15, 2022, 10:37am UTC](https://discourse.julialang.org/t/ann-pdfmerger-jl-produce-multiple-plots-on-separate-pages-in-one-pdf-file/74788/6 "2022-02-15T10:37:13Z")

</div>

Very useful! Let’s see how it develops.

---

<div class="post-metadata">

**Author:** ![draftman9](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/draftman9/32/38128_2.png) [@draftman9](https://discourse.julialang.org/u/draftman9)\
**Post date:** [February 11, 2025, 7:45am UTC](https://discourse.julialang.org/t/ann-pdfmerger-jl-produce-multiple-plots-on-separate-pages-in-one-pdf-file/74788/7 "2025-02-11T07:45:57Z")

</div>

I am also looking for a pkg to merge `Figure` generated by `Makie.jl`. Have you already found one? Thanks anyway. 😃

---

<div class="post-metadata">

**Author:** ![TimG](https://avatars.discourse-cdn.com/v4/letter/t/82dd89/32.png) [@TimG](https://discourse.julialang.org/u/TimG)\
**Post date:** [February 11, 2025, 9:25am UTC](https://discourse.julialang.org/t/ann-pdfmerger-jl-produce-multiple-plots-on-separate-pages-in-one-pdf-file/74788/8 "2025-02-11T09:25:37Z")

</div>

Isn’t this question already answered above?

> [@scheidan1](#):
>
> If you save the Makie figure as PDF (with CairoMakie) it should work.

I have used this approach and it definitely does work!

Or did you need something different?

---

<div class="post-metadata">

**Author:** ![fdekerme](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fdekerme/32/43574_2.png) [@fdekerme](https://discourse.julialang.org/u/fdekerme)\
**Post date:** [February 11, 2025, 2:58pm UTC](https://discourse.julialang.org/t/ann-pdfmerger-jl-produce-multiple-plots-on-separate-pages-in-one-pdf-file/74788/9 "2025-02-11T14:58:02Z")

</div>

Nice profile picture 🙂

---

<div class="post-metadata">

**Author:** ![draftman9](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/draftman9/32/38128_2.png) [@draftman9](https://discourse.julialang.org/u/draftman9)\
**Post date:** [February 11, 2025, 4:57pm UTC](https://discourse.julialang.org/t/ann-pdfmerger-jl-produce-multiple-plots-on-separate-pages-in-one-pdf-file/74788/10 "2025-02-11T16:57:34Z")

</div>

> [@TimG](#):
>
> I have used this approach and it definitely does work!

Alright, I will try it later.

> [@TimG](#):
>
> Or did you need something different?

I mean the `Figure`, a object in Makie.jl generated by functions like `lines` `scatter`. If PDFmerger.jl supports, then I can jump the intermediate saving pdf steps to directly stitch multiple figures. Then total steps only include once operation of saving pdf.
