Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add wicked_pdf_url_base64. #947

Merged
merged 1 commit into from
Oct 30, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions lib/wicked_pdf/wicked_pdf_helper/assets.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,20 @@ def wicked_pdf_asset_base64(path)
"data:#{asset.content_type};base64,#{Rack::Utils.escape(base64)}"
end

# Using `image_tag` with URLs when generating PDFs (specifically large PDFs with lots of pages) can cause buffer/stack overflows.
#
def wicked_pdf_url_base64(url)
response = Net::HTTP.get_response(URI(url))

if response.is_a?(Net::HTTPSuccess)
base64 = Base64.encode64(response.body).gsub(/\s+/, '')
"data:#{response.content_type};base64,#{Rack::Utils.escape(base64)}"
else
Rails.logger.warn("[wicked_pdf] #{response.code} #{response.message}: #{url}")
nil
end
end

def wicked_pdf_stylesheet_link_tag(*sources)
stylesheet_contents = sources.collect do |source|
source = WickedPdfHelper.add_extension(source, 'css')
Expand Down