adzap-wicked_pdf 2.0.0.beta1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (57) hide show
  1. checksums.yaml +7 -0
  2. data/.github/issue_template.md +15 -0
  3. data/.gitignore +21 -0
  4. data/.rubocop.yml +22 -0
  5. data/.rubocop_todo.yml +63 -0
  6. data/.travis.yml +30 -0
  7. data/CHANGELOG.md +135 -0
  8. data/Gemfile +3 -0
  9. data/LICENSE.txt +22 -0
  10. data/README.md +446 -0
  11. data/Rakefile +31 -0
  12. data/gemfiles/4.2.gemfile +6 -0
  13. data/gemfiles/5.0.gemfile +6 -0
  14. data/gemfiles/5.1.gemfile +6 -0
  15. data/gemfiles/5.2.gemfile +9 -0
  16. data/gemfiles/rails_edge.gemfile +9 -0
  17. data/generators/wicked_pdf/templates/wicked_pdf.rb +21 -0
  18. data/generators/wicked_pdf/wicked_pdf_generator.rb +7 -0
  19. data/init.rb +2 -0
  20. data/lib/generators/wicked_pdf_generator.rb +6 -0
  21. data/lib/wicked_pdf.rb +29 -0
  22. data/lib/wicked_pdf/asset_helper.rb +141 -0
  23. data/lib/wicked_pdf/binary.rb +56 -0
  24. data/lib/wicked_pdf/command.rb +52 -0
  25. data/lib/wicked_pdf/document.rb +47 -0
  26. data/lib/wicked_pdf/middleware.rb +101 -0
  27. data/lib/wicked_pdf/option_parser.rb +220 -0
  28. data/lib/wicked_pdf/pdf_helper.rb +17 -0
  29. data/lib/wicked_pdf/progress.rb +33 -0
  30. data/lib/wicked_pdf/railtie.rb +19 -0
  31. data/lib/wicked_pdf/renderer.rb +121 -0
  32. data/lib/wicked_pdf/tempfile.rb +13 -0
  33. data/lib/wicked_pdf/version.rb +3 -0
  34. data/test/dummy/app/assets/javascripts/application.js +16 -0
  35. data/test/dummy/app/assets/javascripts/wicked.js +1 -0
  36. data/test/dummy/app/assets/stylesheets/application.css +15 -0
  37. data/test/dummy/app/assets/stylesheets/wicked.css +1 -0
  38. data/test/dummy/config/database.yml +3 -0
  39. data/test/dummy/config/routes.rb +5 -0
  40. data/test/dummy/log/.gitignore +1 -0
  41. data/test/dummy/public/favicon.ico +0 -0
  42. data/test/fixtures/database.yml +4 -0
  43. data/test/fixtures/document_with_long_line.html +16 -0
  44. data/test/fixtures/wicked.css +1 -0
  45. data/test/fixtures/wicked.js +1 -0
  46. data/test/functional/pdf_helper_test.rb +61 -0
  47. data/test/functional/wicked_pdf_asset_helper_test.rb +118 -0
  48. data/test/test_helper.rb +33 -0
  49. data/test/unit/wicked_pdf_binary_test.rb +52 -0
  50. data/test/unit/wicked_pdf_command_test.rb +4 -0
  51. data/test/unit/wicked_pdf_document_test.rb +60 -0
  52. data/test/unit/wicked_pdf_option_parser_test.rb +128 -0
  53. data/test/unit/wicked_pdf_renderer_test.rb +43 -0
  54. data/test/unit/wicked_pdf_test.rb +8 -0
  55. data/test/unit/wkhtmltopdf_location_test.rb +50 -0
  56. data/wicked_pdf.gemspec +38 -0
  57. metadata +249 -0
@@ -0,0 +1,13 @@
1
+ require 'tempfile'
2
+
3
+ module WickedPdf
4
+ class Tempfile < Tempfile
5
+ # ensures the Tempfile's filename always keeps its extension
6
+ def initialize(filename, temp_dir = nil)
7
+ temp_dir ||= Dir.tmpdir
8
+ extension = File.extname(filename)
9
+ basename = File.basename(filename, extension)
10
+ super([basename, extension], temp_dir)
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,3 @@
1
+ module WickedPdf
2
+ VERSION = '2.0.0.beta1'.freeze
3
+ end
@@ -0,0 +1,16 @@
1
+ // This is a manifest file that'll be compiled into application.js, which will include all the files
2
+ // listed below.
3
+ //
4
+ // Any JavaScript/Coffee file within this directory, lib/assets/javascripts, or any plugin's
5
+ // vendor/assets/javascripts directory can be referenced here using a relative path.
6
+ //
7
+ // It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
8
+ // compiled file. JavaScript code in this file should be added after the last require_* statement.
9
+ //
10
+ // Read Sprockets README (https://github.com/rails/sprockets#sprockets-directives) for details
11
+ // about supported directives.
12
+ //
13
+ //= require rails-ujs
14
+ //= require activestorage
15
+ //= require turbolinks
16
+ //= require_tree .
@@ -0,0 +1 @@
1
+ // Wicked js
@@ -0,0 +1,15 @@
1
+ /*
2
+ * This is a manifest file that'll be compiled into application.css, which will include all the files
3
+ * listed below.
4
+ *
5
+ * Any CSS and SCSS file within this directory, lib/assets/stylesheets, or any plugin's
6
+ * vendor/assets/stylesheets directory can be referenced here using a relative path.
7
+ *
8
+ * You're free to add application-wide styles to this file and they'll appear at the bottom of the
9
+ * compiled file so the styles you add here take precedence over styles defined in any other CSS/SCSS
10
+ * files in this directory. Styles in this file should be added after the last require_* statement.
11
+ * It is generally better to create a new file per style scope.
12
+ *
13
+ *= require_tree .
14
+ *= require_self
15
+ */
@@ -0,0 +1 @@
1
+ /* Wicked styles */
@@ -0,0 +1,3 @@
1
+ test:
2
+ adapter: sqlite3
3
+ database: db/combustion_test.sqlite
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ Rails.application.routes.draw do
4
+ # Add your own routes here, or remove this file if you don't have need for it.
5
+ end
@@ -0,0 +1 @@
1
+ *.log
File without changes
@@ -0,0 +1,4 @@
1
+ test:
2
+ adapter: sqlite3
3
+ database: ":memory:"
4
+ timeout: 500
@@ -0,0 +1,16 @@
1
+ <!DOCTYPE html>
2
+
3
+ <html xmlns="http://www.w3.org/1999/xhtml">
4
+ <body>
5
+ <div>
6
+ Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
7
+ </div>
8
+ </body>
9
+ </html>
10
+
11
+
12
+
13
+
14
+
15
+
16
+
@@ -0,0 +1 @@
1
+ /* Wicked styles */
@@ -0,0 +1 @@
1
+ // Wicked js
@@ -0,0 +1,61 @@
1
+ require 'test_helper'
2
+ require 'wicked_pdf/pdf_helper'
3
+
4
+ module ActionControllerMock
5
+ class Base
6
+ def render_to_string(_)
7
+ [:base]
8
+ end
9
+ end
10
+ end
11
+
12
+ class PdfHelperTest < ActionController::TestCase
13
+ module SomePatch
14
+ def render_to_string(_)
15
+ super.tap do |s|
16
+ s << :patched
17
+ end
18
+ end
19
+ end
20
+
21
+ test 'should not interfere with already prepended patches' do
22
+ # Emulate railtie
23
+ if ActionController::Base.respond_to?(:prepend)
24
+ # this spec tests the following:
25
+ # if another gem prepends a render method to ActionController::Base
26
+ # before wicked_pdf does, does calling render trigger an infinite loop?
27
+ # this spec fails with 6392bea1fe3a41682dfd7c20fd9c179b5a758f59 because PdfHelper
28
+ # aliases the render method prepended by the other gem to render_without_pdf, then
29
+ # base_evals its own definition of render, which calls render_with_pdf -> render_without_pdf.
30
+ # If the other gem uses the prepend inhertinance pattern (calling super instead of aliasing),
31
+ # when it calls super it calls the base_eval'd version of render instead of going up the
32
+ # inheritance chain, causing an infinite loop.
33
+
34
+ # This fiddling with consts is required to get around the fact that PdfHelper checks
35
+ # that it is being prepended to ActionController::Base
36
+ OriginalBase = ActionController::Base
37
+ ActionController.send(:remove_const, :Base)
38
+ ActionController.const_set(:Base, ActionControllerMock::Base)
39
+
40
+ # Emulate another gem being loaded before wicked
41
+ ActionController::Base.prepend(SomePatch)
42
+ ActionController::Base.prepend(::WickedPdf::PdfHelper)
43
+
44
+ begin
45
+ # test that wicked's render method is actually called
46
+ ac = ActionController::Base.new
47
+ ac.expects(:render_to_string)
48
+ ac.render_to_string(:cats)
49
+
50
+ # test that calling render does not trigger infinite loop
51
+ ac = ActionController::Base.new
52
+ assert_equal [:base, :patched], ac.render_to_string(:cats)
53
+ rescue SystemStackError
54
+ assert_equal true, false # force spec failure
55
+ ensure
56
+ ActionController.send(:remove_const, :Base)
57
+ ActionController.const_set(:Base, OriginalBase)
58
+ end
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,118 @@
1
+ require 'test_helper'
2
+ require 'action_view/test_case'
3
+
4
+ class WickedPdfAssetHelperTest < ActionView::TestCase
5
+ include WickedPdf::AssetHelper
6
+
7
+ test 'wicked_pdf_asset_base64 returns a base64 encoded asset' do
8
+ assert_match %r{data:text\/css;base64,.+}, wicked_pdf_asset_base64('wicked.css')
9
+ end
10
+
11
+ test 'wicked_pdf_stylesheet_link_tag should inline the stylesheets passed in' do
12
+ Rails.configuration.assets.expects(:compile => true)
13
+ assert_equal "<style type='text/css'>/* Wicked styles */\n\n</style>", wicked_pdf_stylesheet_link_tag('wicked')
14
+ end
15
+
16
+ test 'wicked_pdf_image_tag should return the same as image_tag when passed a full path' do
17
+ Rails.configuration.assets.expects(:compile => true)
18
+ assert_equal image_tag("file:///#{Rails.root.join('public', 'pdf')}"), wicked_pdf_image_tag('pdf')
19
+ end
20
+
21
+ test 'wicked_pdf_javascript_include_tag should inline the javascripts passed in' do
22
+ Rails.configuration.assets.expects(:compile => true)
23
+ assert_equal "<script type='text/javascript'>// Wicked js\n;\n</script>", wicked_pdf_javascript_include_tag('wicked')
24
+ end
25
+
26
+ test 'wicked_pdf_asset_path should return a url when assets are served by an asset server' do
27
+ expects(:asset_pathname => 'http://assets.domain.com/dummy.png')
28
+ assert_equal 'http://assets.domain.com/dummy.png', wicked_pdf_asset_path('dummy.png')
29
+ end
30
+
31
+ test 'wicked_pdf_asset_path should return a url when assets are served by an asset server using HTTPS' do
32
+ Rails.configuration.assets.expects(:compile => false)
33
+ expects(:asset_path => 'https://assets.domain.com/dummy.png')
34
+ assert_equal 'https://assets.domain.com/dummy.png', wicked_pdf_asset_path('dummy.png')
35
+ end
36
+
37
+ test 'wicked_pdf_asset_path should return a url with a protocol when assets are served by an asset server with relative urls' do
38
+ Rails.configuration.assets.expects(:compile => false)
39
+ expects(:asset_path => '//assets.domain.com/dummy.png')
40
+ assert_equal 'http://assets.domain.com/dummy.png', wicked_pdf_asset_path('dummy.png')
41
+ end
42
+
43
+ test 'wicked_pdf_asset_path should return a url with a protocol when assets are served by an asset server with no protocol set' do
44
+ Rails.configuration.assets.expects(:compile => false)
45
+ expects(:asset_path => 'assets.domain.com/dummy.png')
46
+ assert_equal 'http://assets.domain.com/dummy.png', wicked_pdf_asset_path('dummy.png')
47
+ end
48
+
49
+ test 'wicked_pdf_asset_path should return a path' do
50
+ Rails.configuration.assets.expects(:compile => true)
51
+ path = wicked_pdf_asset_path('application.css')
52
+
53
+ assert path.include?('/app/assets/stylesheets/application.css')
54
+ assert path.include?('file:///')
55
+
56
+ Rails.configuration.assets.expects(:compile => false)
57
+ expects(:asset_path => '/assets/application-6fba03f13d6ff1553477dba03475c4b9b02542e9fb8913bd63c258f4de5b48d9.css')
58
+ path = wicked_pdf_asset_path('application.css')
59
+
60
+ assert path.include?('/public/assets/application-6fba03f13d6ff1553477dba03475c4b9b02542e9fb8913bd63c258f4de5b48d9.css')
61
+ assert path.include?('file:///')
62
+ end
63
+
64
+ # This assets does not exists so probably it doesn't matter what is
65
+ # returned, but lets ensure that returned value is the same when assets
66
+ # are precompiled and when they are not
67
+ test 'wicked_pdf_asset_path should return a path when asset does not exist' do
68
+ Rails.configuration.assets.expects(:compile => true)
69
+ path = wicked_pdf_asset_path('missing.png')
70
+
71
+ assert path.include?('/public/missing.png')
72
+ assert path.include?('file:///')
73
+
74
+ Rails.configuration.assets.expects(:compile => false)
75
+ expects(:asset_path => '/missing.png')
76
+ path = wicked_pdf_asset_path('missing.png')
77
+
78
+ assert path.include?('/public/missing.png')
79
+ assert path.include?('file:///')
80
+ end
81
+
82
+ test 'wicked_pdf_asset_path should return a url when asset is url' do
83
+ Rails.configuration.assets.expects(:compile => true)
84
+ expects(:asset_path => 'http://example.com/rails.png')
85
+ assert_equal 'http://example.com/rails.png', wicked_pdf_asset_path('http://example.com/rails.png')
86
+
87
+ Rails.configuration.assets.expects(:compile => false)
88
+ expects(:asset_path => 'http://example.com/rails.png')
89
+ assert_equal 'http://example.com/rails.png', wicked_pdf_asset_path('http://example.com/rails.png')
90
+ end
91
+
92
+ test 'wicked_pdf_asset_path should return a url when asset is url without protocol' do
93
+ Rails.configuration.assets.expects(:compile => true)
94
+ expects(:asset_path => '//example.com/rails.png')
95
+ assert_equal 'http://example.com/rails.png', wicked_pdf_asset_path('//example.com/rails.png')
96
+
97
+ Rails.configuration.assets.expects(:compile => false)
98
+ expects(:asset_path => '//example.com/rails.png')
99
+ assert_equal 'http://example.com/rails.png', wicked_pdf_asset_path('//example.com/rails.png')
100
+ end
101
+
102
+ test 'AssetHelper::ASSET_URL_REGEX should match various URL data type formats' do
103
+ assert_match WickedPdf::AssetHelper::ASSET_URL_REGEX, 'url(\'/asset/stylesheets/application.css\');'
104
+ assert_match WickedPdf::AssetHelper::ASSET_URL_REGEX, 'url("/asset/stylesheets/application.css");'
105
+ assert_match WickedPdf::AssetHelper::ASSET_URL_REGEX, 'url(/asset/stylesheets/application.css);'
106
+ assert_match WickedPdf::AssetHelper::ASSET_URL_REGEX, 'url(\'http://assets.domain.com/dummy.png\');'
107
+ assert_match WickedPdf::AssetHelper::ASSET_URL_REGEX, 'url("http://assets.domain.com/dummy.png");'
108
+ assert_match WickedPdf::AssetHelper::ASSET_URL_REGEX, 'url(http://assets.domain.com/dummy.png);'
109
+ assert_no_match WickedPdf::AssetHelper::ASSET_URL_REGEX, '.url { \'http://assets.domain.com/dummy.png\' }'
110
+ end
111
+
112
+ test 'prepend_protocol should properly set the protocol when the asset is precompiled' do
113
+ assert_equal 'http://assets.domain.com/dummy.png', prepend_protocol('//assets.domain.com/dummy.png')
114
+ assert_equal '/assets.domain.com/dummy.png', prepend_protocol('/assets.domain.com/dummy.png')
115
+ assert_equal 'http://assets.domain.com/dummy.png', prepend_protocol('http://assets.domain.com/dummy.png')
116
+ assert_equal 'https://assets.domain.com/dummy.png', prepend_protocol('https://assets.domain.com/dummy.png')
117
+ end
118
+ end
@@ -0,0 +1,33 @@
1
+ # Configure Rails Environment
2
+ ENV['RAILS_ENV'] = 'test'
3
+
4
+ require 'combustion'
5
+
6
+ Combustion.path = 'test/dummy'
7
+ Combustion.initialize!(:all) do
8
+ if config.active_record.sqlite3.respond_to?(:represent_boolean_as_integer) # Rails 5.2
9
+ config.active_record.sqlite3.represent_boolean_as_integer = true
10
+ end
11
+ end
12
+
13
+ require 'rails/test_help'
14
+ require 'mocha'
15
+ require 'mocha/mini_test'
16
+
17
+ require 'wicked_pdf'
18
+
19
+ WickedPdf.config = { :exe_path => ENV['WKHTMLTOPDF_BIN'] || '/usr/local/bin/wkhtmltopdf' }
20
+
21
+ Rails.backtrace_cleaner.remove_silencers!
22
+
23
+ if (assets_dir = Rails.root.join('app/assets')) && File.directory?(assets_dir)
24
+ # Copy CSS file
25
+ destination = assets_dir.join('stylesheets/wicked.css')
26
+ source = File.read('test/fixtures/wicked.css')
27
+ File.open(destination, 'w') { |f| f.write(source) }
28
+
29
+ # Copy JS file
30
+ destination = assets_dir.join('javascripts/wicked.js')
31
+ source = File.read('test/fixtures/wicked.js')
32
+ File.open(destination, 'w') { |f| f.write(source) }
33
+ end
@@ -0,0 +1,52 @@
1
+ require 'test_helper'
2
+
3
+ class WickedPdfBinaryTest < ActiveSupport::TestCase
4
+ test 'should extract old wkhtmltopdf version' do
5
+ version_info_sample = "Name:\n wkhtmltopdf 0.9.9\n\nLicense:\n Copyright (C) 2008,2009 Wkhtmltopdf Authors.\n\n\n\n License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.\n This is free software: you are free to change and redistribute it. There is NO\n WARRANTY, to the extent permitted by law.\n\nAuthors:\n Written by Jakob Truelsen. Patches by Mrio Silva, Benoit Garret and Emmanuel\n Bouthenot.\n"
6
+ assert_equal WickedPdf::Binary::DEFAULT_BINARY_VERSION, binary.parse_version_string(version_info_sample)
7
+ end
8
+
9
+ test 'should extract new wkhtmltopdf version' do
10
+ version_info_sample = "Name:\n wkhtmltopdf 0.11.0 rc2\n\nLicense:\n Copyright (C) 2010 wkhtmltopdf/wkhtmltoimage Authors.\n\n\n\n License LGPLv3+: GNU Lesser General Public License version 3 or later\n <http://gnu.org/licenses/lgpl.html>. This is free software: you are free to\n change and redistribute it. There is NO WARRANTY, to the extent permitted by\n law.\n\nAuthors:\n Written by Jan Habermann, Christian Sciberras and Jakob Truelsen. Patches by\n Mehdi Abbad, Lyes Amazouz, Pascal Bach, Emmanuel Bouthenot, Benoit Garret and\n Mario Silva."
11
+ assert_equal Gem::Version.new('0.11.0'), binary.parse_version_string(version_info_sample)
12
+ end
13
+
14
+ test 'should extract wkhtmltopdf version with nondigit symbols' do
15
+ version_info_sample = "Name:\n wkhtmltopdf 0.10.4b\n\nLicense:\n Copyright (C) 2008,2009 Wkhtmltopdf Authors.\n\n\n\n License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.\n This is free software: you are free to change and redistribute it. There is NO\n WARRANTY, to the extent permitted by law.\n\nAuthors:\n Written by Jakob Truelsen. Patches by Mrio Silva, Benoit Garret and Emmanuel\n Bouthenot.\n"
16
+ assert_equal Gem::Version.new('0.10.4b'), binary.parse_version_string(version_info_sample)
17
+ end
18
+
19
+ test 'should fallback to default version on parse error' do
20
+ assert_equal WickedPdf::Binary::DEFAULT_BINARY_VERSION, binary.parse_version_string('')
21
+ end
22
+
23
+ test 'should raise exception when no path to wkhtmltopdf' do
24
+ assert_raise RuntimeError do
25
+ WickedPdf::Binary.new(' ')
26
+ end
27
+ end
28
+
29
+ test 'should raise exception when wkhtmltopdf path is wrong' do
30
+ assert_raise RuntimeError do
31
+ WickedPdf::Binary.new('/i/do/not/exist/notwkhtmltopdf')
32
+ end
33
+ end
34
+
35
+ test 'should raise exception when wkhtmltopdf is not executable' do
36
+ begin
37
+ tmp = Tempfile.new('wkhtmltopdf')
38
+ fp = tmp.path
39
+ File.chmod 0o000, fp
40
+
41
+ assert_raise RuntimeError do
42
+ WickedPdf::Binary.new(fp)
43
+ end
44
+ ensure
45
+ tmp.delete
46
+ end
47
+ end
48
+
49
+ def binary(path = nil)
50
+ WickedPdf::Binary.new(path)
51
+ end
52
+ end
@@ -0,0 +1,4 @@
1
+ require 'test_helper'
2
+
3
+ class WickedPdfCommandTest < ActiveSupport::TestCase
4
+ end
@@ -0,0 +1,60 @@
1
+ require 'test_helper'
2
+
3
+ class WickedPdfDocumentTest < ActiveSupport::TestCase
4
+ HTML_DOCUMENT = '<html><body>Hello World</body></html>'.freeze
5
+
6
+ def setup
7
+ @document = WickedPdf::Document.new
8
+ end
9
+
10
+ test 'should generate PDF from html document' do
11
+ pdf = @document.pdf_from_string HTML_DOCUMENT
12
+ assert pdf.start_with?('%PDF-1.4')
13
+ assert pdf.rstrip.end_with?('%%EOF')
14
+ assert pdf.length > 100
15
+ end
16
+
17
+ test 'should generate PDF from html document with long lines' do
18
+ document_with_long_line_file = File.new('test/fixtures/document_with_long_line.html', 'r')
19
+ pdf = @document.pdf_from_string(document_with_long_line_file.read)
20
+ assert pdf.start_with?('%PDF-1.4')
21
+ assert pdf.rstrip.end_with?('%%EOF')
22
+ assert pdf.length > 100
23
+ end
24
+
25
+ test 'should generate PDF from html existing HTML file without converting it to string' do
26
+ filepath = File.join(Dir.pwd, 'test/fixtures/document_with_long_line.html')
27
+ pdf = @document.pdf_from_html_file(filepath)
28
+ assert pdf.start_with?('%PDF-1.4')
29
+ assert pdf.rstrip.end_with?('%%EOF')
30
+ assert pdf.length > 100
31
+ end
32
+
33
+ test 'should output progress when creating pdfs on compatible hosts' do
34
+ document = WickedPdf::Document.new
35
+ output = []
36
+ options = { :progress => proc { |o| output << o } }
37
+ document.pdf_from_string HTML_DOCUMENT, options
38
+ if RbConfig::CONFIG['target_os'] =~ /mswin|mingw/
39
+ assert_empty output
40
+ else
41
+ assert(output.collect { |l| !l.match(/Loading/).nil? }.include?(true)) # should output something like "Loading pages (1/5)"
42
+ end
43
+ end
44
+
45
+ test 'should raise exception when pdf is empty' do
46
+ begin
47
+ tmp = Tempfile.new('wkhtmltopdf')
48
+ fp = tmp.path
49
+ File.chmod 0o777, fp
50
+ command = WickedPdf::Command.new(binary: WickedPdf::Binary.new(fp))
51
+ document = WickedPdf::Document.new command
52
+
53
+ assert_raise RuntimeError do
54
+ document.pdf_from_string HTML_DOCUMENT
55
+ end
56
+ ensure
57
+ tmp.delete
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,128 @@
1
+ require 'test_helper'
2
+
3
+ class WickedPdfOptionParserTest < ActiveSupport::TestCase
4
+ test 'should parse header and footer options' do
5
+ [:header, :footer].each do |hf|
6
+ [:center, :font_name, :left, :right].each do |o|
7
+ assert_equal "--#{hf}-#{o.to_s.tr('_', '-')} header_footer",
8
+ parse_options(hf => { o => 'header_footer' }).strip
9
+ end
10
+
11
+ [:font_size, :spacing].each do |o|
12
+ assert_equal "--#{hf}-#{o.to_s.tr('_', '-')} 12",
13
+ parse_options(hf => { o => '12' }).strip
14
+ end
15
+
16
+ assert_equal "--#{hf}-line",
17
+ parse_options(hf => { :line => true }).strip
18
+ assert_equal "--#{hf}-html http://www.abc.com",
19
+ parse_options(hf => { :html => { :url => 'http://www.abc.com' } }).strip
20
+ end
21
+ end
22
+
23
+ test 'should parse toc options' do
24
+ toc_option = option_parser.format_option('toc')
25
+
26
+ [:font_name, :header_text].each do |o|
27
+ assert_equal "#{toc_option} --toc-#{o.to_s.tr('_', '-')} toc",
28
+ parse_options(:toc => { o => 'toc' }).strip
29
+ end
30
+
31
+ [
32
+ :depth, :header_fs, :l1_font_size, :l2_font_size, :l3_font_size, :l4_font_size,
33
+ :l5_font_size, :l6_font_size, :l7_font_size, :l1_indentation, :l2_indentation,
34
+ :l3_indentation, :l4_indentation, :l5_indentation, :l6_indentation, :l7_indentation
35
+ ].each do |o|
36
+ assert_equal "#{toc_option} --toc-#{o.to_s.tr('_', '-')} 5",
37
+ parse_options(:toc => { o => 5 }).strip
38
+ end
39
+
40
+ [:no_dots, :disable_links, :disable_back_links].each do |o|
41
+ assert_equal "#{toc_option} --toc-#{o.to_s.tr('_', '-')}",
42
+ parse_options(:toc => { o => true }).strip
43
+ end
44
+ end
45
+
46
+ test 'should parse outline options' do
47
+ assert_equal '--outline', parse_options(:outline => { :outline => true }).strip
48
+ assert_equal '--outline-depth 5', parse_options(:outline => { :outline_depth => 5 }).strip
49
+ end
50
+
51
+ test 'should parse no_images option' do
52
+ assert_equal '--no-images', parse_options(:no_images => true).strip
53
+ assert_equal '--images', parse_options(:images => true).strip
54
+ end
55
+
56
+ test 'should parse margins options' do
57
+ [:top, :bottom, :left, :right].each do |o|
58
+ assert_equal "--margin-#{o} 12", parse_options(:margin => { o => '12' }).strip
59
+ end
60
+ end
61
+
62
+ test 'should parse cover' do
63
+ cover_option = option_parser.format_option('cover')
64
+
65
+ pathname = Rails.root.join('app', 'views', 'pdf', 'file.html')
66
+ assert_equal "#{cover_option} http://example.org", parse_options(:cover => 'http://example.org').strip, 'URL'
67
+ assert_equal "#{cover_option} #{pathname}", parse_options(:cover => pathname).strip, 'Pathname'
68
+ assert_match %r{#{cover_option} .+wicked_cover_pdf.+\.html}, parse_options(:cover => '<html><body>HELLO</body></html>').strip, 'HTML'
69
+ end
70
+
71
+ test 'should parse other options' do
72
+ [
73
+ :orientation, :page_size, :proxy, :username, :password, :dpi,
74
+ :encoding, :user_style_sheet
75
+ ].each do |o|
76
+ assert_equal "--#{o.to_s.tr('_', '-')} opts", parse_options(o => 'opts').strip
77
+ end
78
+
79
+ [:cookie, :post].each do |o|
80
+ assert_equal "--#{o.to_s.tr('_', '-')} name value", parse_options(o => 'name value').strip
81
+
82
+ nv_formatter = proc { |number| "--#{o.to_s.tr('_', '-')} par#{number} val#{number}" }
83
+ assert_equal "#{nv_formatter.call(1)} #{nv_formatter.call(2)}", parse_options(o => ['par1 val1', 'par2 val2']).strip
84
+ end
85
+
86
+ [:redirect_delay, :zoom, :page_offset].each do |o|
87
+ assert_equal "--#{o.to_s.tr('_', '-')} 5", parse_options(o => 5).strip
88
+ end
89
+
90
+ [
91
+ :book, :default_header, :disable_javascript, :grayscale, :lowquality,
92
+ :enable_plugins, :disable_internal_links, :disable_external_links,
93
+ :print_media_type, :disable_smart_shrinking, :use_xserver, :no_background
94
+ ].each do |o|
95
+ assert_equal "--#{o.to_s.tr('_', '-')}", parse_options(o => true).strip
96
+ end
97
+ end
98
+
99
+ test 'should not use double dash options for version without dashes' do
100
+ op = option_parser(WickedPdf::OptionParser::BINARY_VERSION_WITHOUT_DASHES)
101
+
102
+ %w[toc cover].each do |name|
103
+ assert_equal op.format_option(name), name
104
+ end
105
+ end
106
+
107
+ test 'should use double dash options for version with dashes' do
108
+ op = option_parser(Gem::Version.new('0.11.0'))
109
+
110
+ %w[toc cover].each do |name|
111
+ assert_equal op.format_option(name), "--#{name}"
112
+ end
113
+ end
114
+
115
+ test '-- options should not be given after object' do
116
+ options = { :header => { :center => 3 }, :cover => 'http://example.org', :disable_javascript => true }
117
+ cover_option = option_parser.format_option('cover')
118
+ assert_equal parse_options(options), "--disable-javascript --header-center 3 #{cover_option} http://example.org"
119
+ end
120
+
121
+ def parse_options(options, version = WickedPdf::Binary::DEFAULT_BINARY_VERSION)
122
+ option_parser(version).parse(options).join(' ')
123
+ end
124
+
125
+ def option_parser(version = WickedPdf::Binary::DEFAULT_BINARY_VERSION)
126
+ WickedPdf::OptionParser.new(version)
127
+ end
128
+ end