Skip to content

Instantly share code, notes, and snippets.

@nthock
Created October 12, 2017 14:32
Show Gist options
  • Save nthock/170fbfb6279dc8370670c8d269e9cd43 to your computer and use it in GitHub Desktop.
Save nthock/170fbfb6279dc8370670c8d269e9cd43 to your computer and use it in GitHub Desktop.
GSub using Regex
class PdfController < ApplicationController
def export
location = "Singapore"
event = "Jay Chou World Tour Concert Singapore 2017"
raw_svg = '<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" width="300" height="150" viewBox="0 0 300 150" xml:space="preserve">
<desc>Created with Fabric.js 1.7.19</desc>
<defs>
</defs>
<rect x="-30" y="-35" rx="0" ry="0" width="60" height="70" style="stroke: none; stroke-width: 1; stroke-dasharray: none; stroke-linecap: butt; stroke-linejoin: miter; stroke-miterlimit: 10; fill: rgb(255,0,0); fill-rule: nonzero; opacity: 1;" transform="translate(130.5 135.5)"/>
<g transform="translate(164.92 123.1)">
<text xml:space="preserve" font-family="Times New Roman" font-size="40" font-weight="normal" style="stroke: none; stroke-width: 1; stroke-dasharray: none; stroke-linecap: butt; stroke-linejoin: miter; stroke-miterlimit: 10; fill: rgb(0,0,0); fill-rule: nonzero; opacity: 1; white-space: pre;" >
<tspan x="-64.42" y="12.6" fill="rgb(0,0,0)">%{location}</tspan>
</text>
<text xml:space="preserve" font-family="Times New Roman" font-size="40" font-weight="normal" style="stroke: none; stroke-width: 1; stroke-dasharray: none; stroke-linecap: butt; stroke-linejoin: miter; stroke-miterlimit: 10; fill: rgb(0,0,0); fill-rule: nonzero; opacity: 1; white-space: pre;" >
<tspan x="-64.42" y="12.6" fill="rgb(0,0,0)">%{event}</tspan>
</text>
</g>
<g transform="translate(100 100) rotate(30)">
<image xlink:href="http://localhost:3000/admin/campaigns/1/my_image.png" x="0" y="0" style="stroke: none; stroke-width: 0; stroke-dasharray: none; stroke-linecap: butt; stroke-linejoin: miter; stroke-miterlimit: 10; fill: rgb(0,0,0); fill-rule: nonzero; opacity: 0.85;" width="0" height="0" preserveAspectRatio="none"></image>
</g>
</svg>'
processed_svg = raw_svg.gsub(/\%{\location}/, location).gsub(/\%{event}/, event)
our_test_html_file = processed_svg
kit = PDFKit.new(our_test_html_file, :page_size => 'Letter')
file_path = "#{Rails.root}/tmp/hello#{SecureRandom.hex(10)}.pdf"
kit.to_file(file_path)
File.open(file_path)
send_file(file_path, :type => 'application/pdf')
end
end
# Sample SVG file
# <?xml version="1.0" encoding="UTF-8" standalone="no" ?>
# <!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
# <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" width="300" height="150" viewBox="0 0 300 150" xml:space="preserve">
# <desc>Created with Fabric.js 1.7.19</desc>
# <defs>
# </defs>
# <rect x="-30" y="-35" rx="0" ry="0" width="60" height="70" style="stroke: none; stroke-width: 1; stroke-dasharray: none; stroke-linecap: butt; stroke-linejoin: miter; stroke-miterlimit: 10; fill: rgb(255,0,0); fill-rule: nonzero; opacity: 1;" transform="translate(130.5 135.5)"/>
# <g transform="translate(164.92 123.1)">
# <text xml:space="preserve" font-family="Times New Roman" font-size="40" font-weight="normal" style="stroke: none; stroke-width: 1; stroke-dasharray: none; stroke-linecap: butt; stroke-linejoin: miter; stroke-miterlimit: 10; fill: rgb(0,0,0); fill-rule: nonzero; opacity: 1; white-space: pre;" >
# <tspan x="-64.42" y="12.6" fill="rgb(0,0,0)">location</tspan>
# </text>
# </g>
# <g transform="translate(100 100) rotate(30)">
# <image xlink:href="http://localhost:3000/admin/campaigns/1/my_image.png" x="0" y="0" style="stroke: none; stroke-width: 0; stroke-dasharray: none; stroke-linecap: butt; stroke-linejoin: miter; stroke-miterlimit: 10; fill: rgb(0,0,0); fill-rule: nonzero; opacity: 0.85;" width="0" height="0" preserveAspectRatio="none"></image>
# </g>
# </svg>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment