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

Fix to render stylesheets as html safe string on Rails 6 #483

Merged
merged 5 commits into from
Jan 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 6 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
language: ruby

rvm:
- 2.3
- 2.4
Copy link
Contributor Author

@tricknotes tricknotes Jan 6, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I dropped these Ruby versions because they don't work with Rails 6.x.
If you want to keep them, I'll update this PR.

- 2.5
- 2.6

env:
matrix:
- RAILS_VERSION="~> 4.1"
- RAILS_VERSION="~> 5.2"
- RAILS_VERSION="~> 6.0.0"

before_install:
- gem update --system
- gem update bundler
Expand Down
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
source 'https://rubygems.org'

group :test do
gem 'activesupport', '~> 4.1'
gem 'activesupport', ENV['RAILS_VERSION'] || '~> 4.1'
gem 'simplecov', require: false
end

Expand Down
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

Create PDFs using plain old HTML+CSS. Uses [wkhtmltopdf](http://github.com/antialize/wkhtmltopdf) on the back-end which renders HTML using Webkit.

## Supported versions

- Ruby 2.5, 2.6
- Rails 4.2, 5.2, 6.0

## Install

### PDFKit
Expand Down
8 changes: 6 additions & 2 deletions lib/pdfkit/pdfkit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,9 @@ def find_options_in_meta(content)
end

def style_tag_for(stylesheet)
"<style>#{File.read(stylesheet)}</style>"
style = "<style>#{File.read(stylesheet)}</style>"
style = style.html_safe if style.respond_to?(:html_safe)
style
end

def preprocess_html
Expand All @@ -129,7 +131,9 @@ def append_stylesheets

stylesheets.each do |stylesheet|
if @source.to_s.match(/<\/head>/)
@source = Source.new(@source.to_s.gsub(/(<\/head>)/) {|s| style_tag_for(stylesheet) + s })
@source = Source.new(@source.to_s.gsub(/(<\/head>)/) {|s|
style_tag_for(stylesheet) + (s.respond_to?(:html_safe) ? s.html_safe : s)
})
else
@source.to_s.insert(0, style_tag_for(stylesheet))
end
Expand Down
2 changes: 2 additions & 0 deletions pdfkit.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ Gem::Specification.new do |s|
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
s.require_paths = ["lib"]

s.required_ruby_version = '>= 2.5'

s.requirements << "wkhtmltopdf"

# Development Dependencies
Expand Down
8 changes: 8 additions & 0 deletions spec/pdfkit_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -501,6 +501,14 @@
expect(pdfkit.source.to_s).to include("<style>#{File.read(css)}</style></head>")
end

it "can deal with ActiveSupport::SafeBuffer if the HTML doesn't have a head tag" do
pdfkit = PDFKit.new(ActiveSupport::SafeBuffer.new "<html><body>Hai!</body></html>")
css = File.join(SPEC_ROOT,'fixtures','example.css')
pdfkit.stylesheets << css
pdfkit.to_pdf
expect(pdfkit.source.to_s).to include("<style>#{File.read(css)}</style>")
end

it "escapes \\X in stylesheets" do
pdfkit = PDFKit.new("<html><head></head><body>Hai!</body></html>")
css = File.join(SPEC_ROOT,'fixtures','example_with_hex_symbol.css')
Expand Down