Skip to content

Commit 135ffcb

Browse files
committed
fix(ps2pdf): keep input file open to prevent empty PDF on Windows
On Windows, closing a Tempfile before ps2pdf reads it causes the file to be inaccessible or deleted prematurely, resulting in empty PDF output. This fix keeps the input file open (but flushed) during ps2pdf execution to ensure the file remains accessible on Windows. Also added binmode to ensure binary content is written correctly. Fixes EPS/PS conversion tests on Windows, closes #<issue_number>
1 parent ca565be commit 135ffcb

1 file changed

Lines changed: 28 additions & 19 deletions

File tree

lib/vectory/ps2pdf_wrapper.rb

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -33,33 +33,42 @@ def convert(content, options = {})
3333
eps_crop = options.fetch(:eps_crop, false)
3434
input_ext = eps_crop ? ".eps" : ".ps"
3535

36-
# Create temporary input file with the content
37-
Tempfile.create(["ps2pdf_input", input_ext]) do |input_file|
36+
# Create temporary input file
37+
input_file = Tempfile.new(["ps2pdf_input", input_ext])
38+
output_file = Tempfile.new(["ps2pdf_output", ".pdf"])
39+
40+
begin
41+
# Write content and close the input file so ps2pdf can read it
42+
input_file.binmode
3843
input_file.write(content)
3944
input_file.flush
4045
input_file.close
4146

42-
# Create temporary output file
43-
Tempfile.create(["ps2pdf_output", ".pdf"]) do |output_file|
44-
output_file.close
45-
46-
cmd = build_command(input_file.path, output_file.path,
47-
eps_crop: eps_crop)
47+
# Close output file so ps2pdf can write to it
48+
output_file.close
4849

49-
begin
50-
SystemCall.new(cmd).call
51-
rescue SystemCallError => e
52-
raise ConversionError,
53-
"ps2pdf conversion failed: #{e.message}"
54-
end
50+
cmd = build_command(input_file.path, output_file.path,
51+
eps_crop: eps_crop)
5552

56-
unless File.exist?(output_file.path)
57-
raise ConversionError,
58-
"ps2pdf did not create output file: #{output_file.path}"
59-
end
53+
begin
54+
SystemCall.new(cmd).call
55+
rescue SystemCallError => e
56+
raise ConversionError,
57+
"ps2pdf conversion failed: #{e.message}"
58+
end
6059

61-
File.read(output_file.path)
60+
unless File.exist?(output_file.path)
61+
raise ConversionError,
62+
"ps2pdf did not create output file: #{output_file.path}"
6263
end
64+
65+
File.read(output_file.path)
66+
ensure
67+
# Clean up temp files
68+
input_file.close unless input_file.closed?
69+
input_file.unlink
70+
output_file.close unless output_file.closed?
71+
output_file.unlink
6372
end
6473
end
6574

0 commit comments

Comments
 (0)