This repository has been archived by the owner on Dec 15, 2023. It is now read-only.
forked from gmonfort/pdf_machine
-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.rb
59 lines (47 loc) · 1.49 KB
/
app.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
require "cuba"
require "cuba/render"
require 'cuba/send_file'
require "rack/protection"
require "tilt/erb"
require "pdf_machine"
Cuba.use Rack::Session::Cookie, :secret => "9461c163cbc13617358e50fdbd24ec7fd0ab1e099ee7ee121a0237e0d98047b0b8b23fb806dc2bcfa697d75c383d46c92a79c5d948578ae34eb0487db30a8120"
Cuba.use Rack::Protection
Cuba.use Rack::Protection::RemoteReferrer
Cuba.plugin Cuba::Render
Cuba.plugin Cuba::SendFile
Cuba.define do
on root do
res.redirect "/pdfs"
end
on get, "pdfs" do
res.write view('pdfs/form')
end
on post, "pdfs", param("svg"), param("batik"), param("rotate_pdf") do |svg_data, batik, rotate_pdf|
begin
file = svg_data[:tempfile].path
blob = if batik == "1"
PDFMachine.convert_svg_using_batik(file, :pdf)
else
PDFMachine.convert_svg(file, :pdf, {rotate_pdf: rotate_pdf})
end
res.headers['Content-Type'] = 'application/pdf'
res.headers['Content-Disposition'] = 'attachment; filename="out.pdf"'
send_file(blob.path)
rescue => e
puts "****** ERROR --- #{e.inspect}"
res.redirect "/pdfs"
end
end
on post, "pngs", param("svg") do |svg_data|
begin
file = svg_data[:tempfile].path
blob = PDFMachine.convert_svg(file, :png)
res.headers['Content-Type'] = 'image/png'
res.headers['Content-Disposition'] = 'attachment; filename="out.png"'
send_file(blob.path)
rescue => e
puts "****** ERROR --- #{e.inspect}"
res.redirect "/pngs"
end
end
end