Looking to do the same thing, looks like it might be easier to make changes in the save
function, between the screen = getscreen
and backend_show
call
using CairoMakie
using CairoMakie.Makie: FigureLike, current_backend, update_state_before_display!, getscreen, get_scene, filetype, isvisible, backend_show
using CairoMakie.Makie.FileIO
CairoMakie.activate!()
function save_pdf(
filename::String, fig::FigureLike; args...
)
save_pdf(FileIO.query(filename), fig; args...)
end
function save_pdf(
file::FileIO.Formatted, fig::FigureLike;
size=Base.size(get_scene(fig)),
resolution=nothing,
backend=current_backend(),
update=true,
pdf_vers=1,
screen_config...
)
scene = get_scene(fig)
if resolution !== nothing
@warn "The keyword argument `resolution` for `save()` has been deprecated. Use `size` instead, which better reflects that this is a unitless size and not a pixel resolution."
size = resolution
end
if size != Base.size(scene)
resize!(scene, size)
end
filename = FileIO.filename(file)
# Delete previous file if it exists and query only the file string for type.
# We overwrite existing files anyway, so this doesn't change the behavior.
# But otherwise we could get a filetype :UNKNOWN from a corrupt existing file
# (from an error during save, e.g.), therefore we don't want to rely on the
# type readout from an existing file.
isfile(filename) && rm(filename)
# query the filetype only from the file extension
F = filetype(file)
mime = MIME("application/pdf")
try
return open(filename, "w") do io
# If the scene already got displayed, we get the current screen its displayed on
# Else, we create a new scene and update the state of the fig
update && update_state_before_display!(fig)
visible = isvisible(getscreen(scene)) # if already has a screen, don't hide it!
config = Dict{Symbol,Any}(screen_config)
get!(config, :visible, visible)
screen = getscreen(backend, scene, config, io, mime)
ccall((:cairo_pdf_surface_restrict_to_version, CairoMakie.Cairo.libcairo), Nothing,
(Ptr{UInt8}, Int32), screen.surface.ptr, pdf_vers)
backend_show(screen, io, mime, scene)
end
catch e
# So, if open(io-> error(...), "w"), the file will get created, but not removed...
isfile(filename) && rm(filename; force=true)
rethrow(e)
end
end
# ---
begin
fig = Figure()
ax = Axis(fig[1, 1])
x = 0:0.05:pi
lines!(x, sin.(x))
fig
end
save_pdf("test2.pdf", fig)
gives
pdfinfo test2.pdf 09:04:35 PM
Producer: cairo 1.18.0 (https://cairographics.org)
CreationDate: Wed May 8 21:09:23 2024 EDT
Custom Metadata: no
Metadata Stream: no
Tagged: no
UserProperties: no
Suspects: no
Form: none
JavaScript: no
Pages: 1
Encrypted: no
Page size: 450 x 338 pts
Page rot: 0
File size: 4071 bytes
Optimized: no
PDF version: 1.5