diff --git a/.github/workflows/package.yml b/.github/workflows/package.yml index d822ad8..8cf18f2 100644 --- a/.github/workflows/package.yml +++ b/.github/workflows/package.yml @@ -74,9 +74,9 @@ jobs: - name: Smoke test native Linux gem in clean Ruby image run: | - docker run --rm -v "$PWD/pkg:/pkg" ruby:3.2-slim bash -lc 'apt-get update && apt-get install -y --no-install-recommends build-essential && gem install /pkg/page_print-*-x86_64-linux.gem || { find /usr/local/bundle/extensions -name mkmf.log -print -exec cat {} \;; exit 1; }; ruby -e "require \"page_print\"; pdf = PagePrint.html_to_pdf_string(\"

Hello

\"); abort \"bad pdf\" unless pdf.start_with?(\"%PDF\"); puts \"ok\""' - docker run --rm -v "$PWD/pkg:/pkg" ruby:3.4.7-slim bash -lc 'apt-get update && apt-get install -y --no-install-recommends build-essential && gem install /pkg/page_print-*-x86_64-linux.gem || { find /usr/local/bundle/extensions -name mkmf.log -print -exec cat {} \;; exit 1; }; ruby -e "require \"page_print\"; pdf = PagePrint.html_to_pdf_string(\"

Hello

\"); abort \"bad pdf\" unless pdf.start_with?(\"%PDF\"); puts \"ok\""' - docker run --rm -v "$PWD/pkg:/pkg" ruby:4.0-slim bash -lc 'apt-get update && apt-get install -y --no-install-recommends build-essential && gem install /pkg/page_print-*-x86_64-linux.gem || { find /usr/local/bundle/extensions -name mkmf.log -print -exec cat {} \;; exit 1; }; ruby -e "require \"page_print\"; pdf = PagePrint.html_to_pdf_string(\"

Hello

\"); abort \"bad pdf\" unless pdf.start_with?(\"%PDF\"); puts \"ok\""' + docker run --rm -v "$PWD/pkg:/pkg" ruby:3.2-slim bash -lc 'apt-get update && apt-get install -y --no-install-recommends build-essential && gem install /pkg/page_print-*-x86_64-linux.gem || { find /usr/local/bundle/extensions -name mkmf.log -print -exec cat {} \;; exit 1; }; ruby -e "require \"page_print\"; pdf = PagePrint.render(\"

Hello

\"); abort \"bad pdf\" unless pdf.start_with?(\"%PDF\"); puts \"ok\""' + docker run --rm -v "$PWD/pkg:/pkg" ruby:3.4.7-slim bash -lc 'apt-get update && apt-get install -y --no-install-recommends build-essential && gem install /pkg/page_print-*-x86_64-linux.gem || { find /usr/local/bundle/extensions -name mkmf.log -print -exec cat {} \;; exit 1; }; ruby -e "require \"page_print\"; pdf = PagePrint.render(\"

Hello

\"); abort \"bad pdf\" unless pdf.start_with?(\"%PDF\"); puts \"ok\""' + docker run --rm -v "$PWD/pkg:/pkg" ruby:4.0-slim bash -lc 'apt-get update && apt-get install -y --no-install-recommends build-essential && gem install /pkg/page_print-*-x86_64-linux.gem || { find /usr/local/bundle/extensions -name mkmf.log -print -exec cat {} \;; exit 1; }; ruby -e "require \"page_print\"; pdf = PagePrint.render(\"

Hello

\"); abort \"bad pdf\" unless pdf.start_with?(\"%PDF\"); puts \"ok\""' - uses: actions/upload-artifact@v4 with: @@ -112,7 +112,7 @@ jobs: - name: Smoke test native Apple Silicon macOS gem run: | gem install pkg/page_print-*-arm64-darwin.gem - ruby -e 'require "page_print"; pdf = PagePrint.html_to_pdf_string("

Hello

"); abort "bad pdf" unless pdf.start_with?("%PDF"); puts "ok"' + ruby -e 'require "page_print"; pdf = PagePrint.render("

Hello

"); abort "bad pdf" unless pdf.start_with?("%PDF"); puts "ok"' - uses: actions/upload-artifact@v4 with: diff --git a/AGENTS.md b/AGENTS.md index 42ded4c..f218b87 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -4,7 +4,7 @@ `page_print` is a Ruby gem with a native C extension that renders HTML strings to PDF files using the `plutobook` C library. -The public Ruby API is intentionally small. `PagePrint.html_to_pdf` is the primary entry point. +The public Ruby API is intentionally small. `PagePrint.render` is the primary entry point. ## Repository Layout @@ -59,7 +59,7 @@ Use `bin/console` for a local development console. It compiles the extension bef ## Development Guidelines - Keep the Ruby-facing API minimal and documented in `README.md`. -- When changing `PagePrint.html_to_pdf`, update tests for both successful output and validation/error behavior. +- When changing `PagePrint.render` or `PagePrint.render_to_file`, update tests for both successful output and validation/error behavior. - Prefer inline Ruby calls for readability; only break Ruby argument lists across lines when the line would exceed 120 characters. - Format Ruby private sections with `private` at the class indentation level and private method definitions indented beneath it, without a blank line between `private` and the first private method. - Validate Ruby argument types before passing data into `plutobook`. diff --git a/README.md b/README.md index f7a12ef..059c994 100644 --- a/README.md +++ b/README.md @@ -33,7 +33,7 @@ Render a PDF from a controller: class PrintsController < ApplicationController def pdf html = render_to_string(template: "prints/pdf", formats: [:html], layout: "pdf") - pdf = PagePrint.html_to_pdf_string( + pdf = PagePrint.render( html, page_size: :a4, margins: :normal, @@ -58,7 +58,7 @@ During controller actions, PagePrint defaults `base_url` to `request.base_url`. You can still override `base_url` explicitly: ```ruby -pdf = PagePrint.html_to_pdf_string(html, base_url: "https://example.com") +pdf = PagePrint.render(html, base_url: "https://example.com") ``` Override the fetcher only when needed: @@ -87,19 +87,19 @@ HTML output_path = File.join(Dir.tmpdir, "page_print-output.pdf") -PagePrint.html_to_pdf(html, output_path, base_url: "https://example.com") +PagePrint.render_to_file(html, output_path, base_url: "https://example.com") ``` To get the generated PDF as a binary string instead of writing directly to a file: ```ruby -pdf = PagePrint.html_to_pdf_string(html, base_url: "https://example.com") +pdf = PagePrint.render(html, base_url: "https://example.com") ``` Use custom page dimensions and margins when a preset is not enough: ```ruby -pdf = PagePrint.html_to_pdf_string( +pdf = PagePrint.render( html, page_size: { width: 100, height: 150, unit: :mm }, margins: { top: 5, right: 6, bottom: 7, left: 8, unit: :mm } @@ -181,7 +181,7 @@ Native gems bundle PlutoBook and required non-system shared libraries. Optional ## Notes -- The extension supports writing to a file path with `html_to_pdf` or returning PDF bytes with `html_to_pdf_string`. +- The extension supports writing to a file path with `render_to_file` or returning PDF bytes with `render`. - JavaScript execution is intentionally unsupported. - Native gems disable PlutoBook's optional curl, TurboJPEG, and WebP features. @@ -213,13 +213,13 @@ require "tmpdir" output_path = File.join(Dir.tmpdir, "page_print-output.pdf") -PagePrint.html_to_pdf("

Hello

", output_path, page_size: :letter, margins: :narrow, media: :screen) +PagePrint.render_to_file("

Hello

", output_path, page_size: :letter, margins: :narrow, media: :screen) ``` You can also do a quick one-shot smoke test from the shell: ```sh -bundle exec ruby -Ilib -e 'require "tmpdir"; require "page_print"; output_path = File.join(Dir.tmpdir, "page_print-output.pdf"); p PagePrint.html_to_pdf("

Hello

", output_path, page_size: :letter, margins: :narrow, media: :screen)' +bundle exec ruby -Ilib -e 'require "tmpdir"; require "page_print"; output_path = File.join(Dir.tmpdir, "page_print-output.pdf"); p PagePrint.render_to_file("

Hello

", output_path, page_size: :letter, margins: :narrow, media: :screen)' ``` Or use the development console, which compiles the native extension first and then starts IRB with `PagePrint` loaded: diff --git a/benchmark/pdf_renderers.rb b/benchmark/pdf_renderers.rb index 4f33d53..897a256 100644 --- a/benchmark/pdf_renderers.rb +++ b/benchmark/pdf_renderers.rb @@ -120,7 +120,7 @@ def pdfkit_options end def render_page_print(html, output_path) - PagePrint.html_to_pdf(html, output_path, **page_print_options) + PagePrint.render_to_file(html, output_path, **page_print_options) end def render_pdfkit(html, output_path) diff --git a/ext/page_print/page_print.c b/ext/page_print/page_print.c index 95c46fa..4bafcee 100644 --- a/ext/page_print/page_print.c +++ b/ext/page_print/page_print.c @@ -739,7 +739,7 @@ static plutobook_stream_status_t pageprint_write_pdf_string(void *closure, const return PLUTOBOOK_STREAM_STATUS_SUCCESS; } -static VALUE pageprint_html_to_pdf(int argc, VALUE *argv, VALUE self) { +static VALUE pageprint_render_to_file(int argc, VALUE *argv, VALUE self) { VALUE html; VALUE path; VALUE options; @@ -799,7 +799,7 @@ static VALUE pageprint_html_to_pdf(int argc, VALUE *argv, VALUE self) { return Qtrue; } -static VALUE pageprint_html_to_pdf_string(int argc, VALUE *argv, VALUE self) { +static VALUE pageprint_render(int argc, VALUE *argv, VALUE self) { VALUE html; VALUE options; pageprint_pdf_string_output_t output; @@ -888,6 +888,6 @@ void Init_page_print(void) { id_mm = rb_intern_const("mm"); id_px = rb_intern_const("px"); - rb_define_singleton_method(mPagePrint, "html_to_pdf", RUBY_METHOD_FUNC(pageprint_html_to_pdf), -1); - rb_define_singleton_method(mPagePrint, "html_to_pdf_string", RUBY_METHOD_FUNC(pageprint_html_to_pdf_string), -1); + rb_define_singleton_method(mPagePrint, "render_to_file", RUBY_METHOD_FUNC(pageprint_render_to_file), -1); + rb_define_singleton_method(mPagePrint, "render", RUBY_METHOD_FUNC(pageprint_render), -1); } diff --git a/test/page_print_test.rb b/test/page_print_test.rb index b761db4..c73ff49 100644 --- a/test/page_print_test.rb +++ b/test/page_print_test.rb @@ -105,7 +105,7 @@ def test_configured_rails_resource_fetcher_works_with_pdf_rendering File.binwrite(File.join(assets_dir, 'pdf.css'), 'body { color: green; }') PagePrint.resource_fetcher = PagePrint::RailsResourceFetcher.new(rails: fake_rails(public_dir)) - pdf = PagePrint.html_to_pdf_string( + pdf = PagePrint.render( '

Hello

', base_url: 'http://example.com' ) @@ -115,12 +115,12 @@ def test_configured_rails_resource_fetcher_works_with_pdf_rendering end end - def test_html_to_pdf_string_uses_configured_base_url + def test_render_uses_configured_base_url urls = [] PagePrint.base_url = 'http://example.com' - pdf = PagePrint.html_to_pdf_string( + pdf = PagePrint.render( '

Hello

', resource_fetcher: lambda { |url| urls << url @@ -133,11 +133,11 @@ def test_html_to_pdf_string_uses_configured_base_url assert_equal '%PDF', pdf.byteslice(0, 4) end - def test_html_to_pdf_string_uses_request_local_base_url + def test_render_uses_request_local_base_url urls = [] pdf = PagePrint.with_base_url('http://request.example') do - PagePrint.html_to_pdf_string( + PagePrint.render( '

Hello

', resource_fetcher: lambda { |url| urls << url @@ -161,11 +161,11 @@ def test_with_base_url_restores_previous_base_url assert_equal 'http://configured.example', PagePrint.base_url end - def test_html_to_pdf_string_does_not_override_explicit_base_url + def test_render_does_not_override_explicit_base_url urls = [] pdf = PagePrint.with_base_url('http://request.example') do - PagePrint.html_to_pdf_string( + PagePrint.render( '

Hello

', base_url: 'http://explicit.example', resource_fetcher: lambda { |url| @@ -180,53 +180,53 @@ def test_html_to_pdf_string_does_not_override_explicit_base_url assert_equal '%PDF', pdf.byteslice(0, 4) end - def test_html_to_pdf_requires_html_to_be_a_string + def test_render_to_file_requires_html_to_be_a_string error = assert_raises(TypeError) do - PagePrint.html_to_pdf(123, 'output.pdf') + PagePrint.render_to_file(123, 'output.pdf') end assert_equal 'html must be a String', error.message end - def test_html_to_pdf_requires_path_to_be_a_string + def test_render_to_file_requires_path_to_be_a_string error = assert_raises(TypeError) do - PagePrint.html_to_pdf('

Hello

', 123) + PagePrint.render_to_file('

Hello

', 123) end assert_equal 'path must be a String', error.message end - def test_html_to_pdf_rejects_empty_html + def test_render_to_file_rejects_empty_html error = assert_raises(ArgumentError) do - PagePrint.html_to_pdf('', 'output.pdf') + PagePrint.render_to_file('', 'output.pdf') end assert_equal 'html must not be empty', error.message end - def test_html_to_pdf_rejects_empty_path + def test_render_to_file_rejects_empty_path error = assert_raises(ArgumentError) do - PagePrint.html_to_pdf('

Hello

', '') + PagePrint.render_to_file('

Hello

', '') end assert_equal 'path must not be empty', error.message end - def test_html_to_pdf_writes_output_file + def test_render_to_file_writes_output_file Dir.mktmpdir do |dir| output_path = File.join(dir, 'output.pdf') - assert PagePrint.html_to_pdf('

Hello

', output_path) + assert PagePrint.render_to_file('

Hello

', output_path) assert File.exist?(output_path) assert_operator File.size(output_path), :>, 0 end end - def test_html_to_pdf_accepts_keyword_options + def test_render_to_file_accepts_keyword_options Dir.mktmpdir do |dir| output_path = File.join(dir, 'output.pdf') - assert PagePrint.html_to_pdf( + assert PagePrint.render_to_file( '

Hello

', output_path, base_url: 'https://example.com', @@ -240,11 +240,11 @@ def test_html_to_pdf_accepts_keyword_options end end - def test_html_to_pdf_accepts_custom_page_size_and_margins + def test_render_to_file_accepts_custom_page_size_and_margins Dir.mktmpdir do |dir| output_path = File.join(dir, 'output.pdf') - assert PagePrint.html_to_pdf( + assert PagePrint.render_to_file( '

Hello

', output_path, page_size: { width: 100, height: 150, unit: :mm }, @@ -257,11 +257,11 @@ def test_html_to_pdf_accepts_custom_page_size_and_margins end end - def test_html_to_pdf_accepts_metadata + def test_render_to_file_accepts_metadata Dir.mktmpdir do |dir| output_path = File.join(dir, 'output.pdf') - assert PagePrint.html_to_pdf( + assert PagePrint.render_to_file( '

Hello

', output_path, metadata: { @@ -285,8 +285,8 @@ def test_html_to_pdf_accepts_metadata end end - def test_html_to_pdf_string_returns_pdf_bytes - pdf = PagePrint.html_to_pdf_string('

Hello

') + def test_render_returns_pdf_bytes + pdf = PagePrint.render('

Hello

') assert_instance_of String, pdf assert_equal Encoding::ASCII_8BIT, pdf.encoding @@ -294,8 +294,8 @@ def test_html_to_pdf_string_returns_pdf_bytes assert_equal '%PDF', pdf.byteslice(0, 4) end - def test_html_to_pdf_string_accepts_keyword_options - pdf = PagePrint.html_to_pdf_string( + def test_render_accepts_keyword_options + pdf = PagePrint.render( '

Hello

', base_url: 'https://example.com', page_size: :letter, @@ -307,8 +307,8 @@ def test_html_to_pdf_string_accepts_keyword_options assert_equal '%PDF', pdf.byteslice(0, 4) end - def test_html_to_pdf_string_accepts_metadata - pdf = PagePrint.html_to_pdf_string( + def test_render_accepts_metadata + pdf = PagePrint.render( '

Hello

', metadata: { title: 'Test PDF', @@ -324,8 +324,8 @@ def test_html_to_pdf_string_accepts_metadata assert_equal '%PDF', pdf.byteslice(0, 4) end - def test_html_to_pdf_string_accepts_nil_metadata - pdf = PagePrint.html_to_pdf_string( + def test_render_accepts_nil_metadata + pdf = PagePrint.render( '

Hello

', metadata: nil ) @@ -334,52 +334,52 @@ def test_html_to_pdf_string_accepts_nil_metadata assert_equal '%PDF', pdf.byteslice(0, 4) end - def test_html_to_pdf_string_requires_metadata_to_be_a_hash_or_nil + def test_render_requires_metadata_to_be_a_hash_or_nil error = assert_raises(TypeError) do - PagePrint.html_to_pdf_string('

Hello

', metadata: 'title') + PagePrint.render('

Hello

', metadata: 'title') end assert_equal 'metadata must be a Hash or nil', error.message end - def test_html_to_pdf_string_requires_metadata_keys_to_be_symbols + def test_render_requires_metadata_keys_to_be_symbols error = assert_raises(TypeError) do - PagePrint.html_to_pdf_string('

Hello

', metadata: { 'title' => 'Test PDF' }) + PagePrint.render('

Hello

', metadata: { 'title' => 'Test PDF' }) end assert_equal 'metadata keys must be Symbols', error.message end - def test_html_to_pdf_string_rejects_unknown_metadata_keys + def test_render_rejects_unknown_metadata_keys error = assert_raises(ArgumentError) do - PagePrint.html_to_pdf_string('

Hello

', metadata: { publisher: 'PagePrint' }) + PagePrint.render('

Hello

', metadata: { publisher: 'PagePrint' }) end assert_equal 'metadata contains unknown key: :publisher', error.message end - def test_html_to_pdf_string_rejects_creator_metadata + def test_render_rejects_creator_metadata error = assert_raises(ArgumentError) do - PagePrint.html_to_pdf_string('

Hello

', metadata: { creator: 'PagePrint' }) + PagePrint.render('

Hello

', metadata: { creator: 'PagePrint' }) end assert_equal 'metadata contains unknown key: :creator', error.message end - def test_html_to_pdf_string_requires_metadata_values_to_be_strings_or_nil + def test_render_requires_metadata_values_to_be_strings_or_nil error = assert_raises(TypeError) do - PagePrint.html_to_pdf_string('

Hello

', metadata: { title: 123 }) + PagePrint.render('

Hello

', metadata: { title: 123 }) end assert_equal 'metadata values must be Strings or nil', error.message end - def test_html_to_pdf_string_reraises_metadata_errors_after_html_load + def test_render_reraises_metadata_errors_after_html_load metadata = { title: 'Test PDF' } html = '

Hello

' assert_raises(TypeError) do - PagePrint.html_to_pdf_string( + PagePrint.render( html, metadata: metadata, resource_fetcher: lambda { |_url| @@ -389,13 +389,13 @@ def test_html_to_pdf_string_reraises_metadata_errors_after_html_load ) end - pdf = PagePrint.html_to_pdf_string('

Hello

') + pdf = PagePrint.render('

Hello

') assert_equal '%PDF', pdf.byteslice(0, 4) end - def test_html_to_pdf_string_uses_resource_fetcher + def test_render_uses_resource_fetcher urls = [] - pdf = PagePrint.html_to_pdf_string( + pdf = PagePrint.render( '

Hello

', resource_fetcher: lambda { |url| urls << url @@ -408,14 +408,14 @@ def test_html_to_pdf_string_uses_resource_fetcher assert_equal '%PDF', pdf.byteslice(0, 4) end - def test_html_to_pdf_string_uses_configured_resource_fetcher + def test_render_uses_configured_resource_fetcher urls = [] PagePrint.resource_fetcher = lambda { |url| urls << url { content: 'body { color: blue; }', mime_type: 'text/css' } } - pdf = PagePrint.html_to_pdf_string( + pdf = PagePrint.render( '

Hello

' ) @@ -424,17 +424,17 @@ def test_html_to_pdf_string_uses_configured_resource_fetcher assert_equal '%PDF', pdf.byteslice(0, 4) end - def test_html_to_pdf_string_requires_resource_fetcher_to_respond_to_call + def test_render_requires_resource_fetcher_to_respond_to_call error = assert_raises(TypeError) do - PagePrint.html_to_pdf_string('

Hello

', resource_fetcher: Object.new) + PagePrint.render('

Hello

', resource_fetcher: Object.new) end assert_equal 'resource_fetcher must respond to call or be nil/false', error.message end - def test_html_to_pdf_string_requires_resource_fetcher_result_to_be_a_hash_or_nil + def test_render_requires_resource_fetcher_result_to_be_a_hash_or_nil error = assert_raises(TypeError) do - PagePrint.html_to_pdf_string( + PagePrint.render( '

Hello

', resource_fetcher: ->(_url) { [] } ) @@ -443,9 +443,9 @@ def test_html_to_pdf_string_requires_resource_fetcher_result_to_be_a_hash_or_nil assert_equal 'resource_fetcher must return a Hash or nil', error.message end - def test_html_to_pdf_string_requires_resource_fetcher_content_to_be_a_string + def test_render_requires_resource_fetcher_content_to_be_a_string error = assert_raises(TypeError) do - PagePrint.html_to_pdf_string( + PagePrint.render( '

Hello

', resource_fetcher: ->(_url) { { content: nil, mime_type: 'text/css' } } ) @@ -454,12 +454,12 @@ def test_html_to_pdf_string_requires_resource_fetcher_content_to_be_a_string assert_equal 'resource_fetcher result content must be a String', error.message end - def test_html_to_pdf_string_rejects_resource_fetcher_content_larger_than_uint_max + def test_render_rejects_resource_fetcher_content_larger_than_uint_max html = '' content = oversized_string(2**32) error = assert_raises(ArgumentError) do - PagePrint.html_to_pdf_string( + PagePrint.render( html, resource_fetcher: ->(_url) { { content: content, mime_type: 'text/css' } } ) @@ -468,9 +468,9 @@ def test_html_to_pdf_string_rejects_resource_fetcher_content_larger_than_uint_ma assert_equal 'resource_fetcher result content is too large', error.message end - def test_html_to_pdf_string_requires_resource_fetcher_mime_type_to_be_a_string + def test_render_requires_resource_fetcher_mime_type_to_be_a_string error = assert_raises(TypeError) do - PagePrint.html_to_pdf_string( + PagePrint.render( '

Hello

', resource_fetcher: ->(_url) { { content: 'body {}', mime_type: nil } } ) @@ -479,9 +479,9 @@ def test_html_to_pdf_string_requires_resource_fetcher_mime_type_to_be_a_string assert_equal 'resource_fetcher result mime_type must be a String', error.message end - def test_html_to_pdf_string_reraises_resource_fetcher_errors + def test_render_reraises_resource_fetcher_errors error = assert_raises(RuntimeError) do - PagePrint.html_to_pdf_string( + PagePrint.render( '

Hello

', resource_fetcher: ->(_url) { raise 'fetch failed' } ) @@ -490,11 +490,11 @@ def test_html_to_pdf_string_reraises_resource_fetcher_errors assert_equal 'fetch failed', error.message end - def test_html_to_pdf_string_denies_network_fetch_when_resource_fetcher_returns_nil + def test_render_denies_network_fetch_when_resource_fetcher_returns_nil html = '' urls = [] - pdf = PagePrint.html_to_pdf_string( + pdf = PagePrint.render( html, resource_fetcher: ->(url) { urls << url @@ -507,82 +507,82 @@ def test_html_to_pdf_string_denies_network_fetch_when_resource_fetcher_returns_n assert_equal '%PDF', pdf.byteslice(0, 4) end - def test_html_to_pdf_string_denies_network_fetch_without_resource_fetcher + def test_render_denies_network_fetch_without_resource_fetcher html = '' - pdf = PagePrint.html_to_pdf_string(html) + pdf = PagePrint.render(html) assert_operator pdf.bytesize, :>, 0 assert_equal '%PDF', pdf.byteslice(0, 4) end - def test_html_to_pdf_string_requires_html_to_be_a_string + def test_render_requires_html_to_be_a_string error = assert_raises(TypeError) do - PagePrint.html_to_pdf_string(123) + PagePrint.render(123) end assert_equal 'html must be a String', error.message end - def test_html_to_pdf_string_rejects_empty_html + def test_render_rejects_empty_html error = assert_raises(ArgumentError) do - PagePrint.html_to_pdf_string('') + PagePrint.render('') end assert_equal 'html must not be empty', error.message end - def test_html_to_pdf_string_rejects_html_larger_than_int_max + def test_render_rejects_html_larger_than_int_max html = oversized_string(2**31) error = assert_raises(ArgumentError) do - PagePrint.html_to_pdf_string(html) + PagePrint.render(html) end assert_equal 'html is too large', error.message end - def test_html_to_pdf_string_rejects_invalid_options + def test_render_rejects_invalid_options error = assert_raises(TypeError) do - PagePrint.html_to_pdf_string('

Hello

', :options) + PagePrint.render('

Hello

', :options) end assert_equal 'options must be a Hash', error.message end - def test_html_to_pdf_string_rejects_unknown_keyword + def test_render_rejects_unknown_keyword error = assert_raises(ArgumentError) do - PagePrint.html_to_pdf_string('

Hello

', foo: :bar) + PagePrint.render('

Hello

', foo: :bar) end assert_equal 'unknown keyword: :foo', error.message end - def test_html_to_pdf_string_rejects_invalid_media + def test_render_rejects_invalid_media error = assert_raises(ArgumentError) do - PagePrint.html_to_pdf_string('

Hello

', media: :speech) + PagePrint.render('

Hello

', media: :speech) end assert_equal 'media must be one of: :print, :screen', error.message end - def test_html_to_pdf_includes_path_when_pdf_write_fails + def test_render_to_file_includes_path_when_pdf_write_fails Dir.mktmpdir do |dir| output_path = File.join(dir, 'missing', 'output.pdf') error = assert_raises(RuntimeError) do - PagePrint.html_to_pdf('

Hello

', output_path) + PagePrint.render_to_file('

Hello

', output_path) end assert_match(/\Afailed to write PDF to #{Regexp.escape(output_path)}/, error.message) end end - def test_html_to_pdf_accepts_nil_base_url + def test_render_to_file_accepts_nil_base_url Dir.mktmpdir do |dir| output_path = File.join(dir, 'output.pdf') - assert PagePrint.html_to_pdf( + assert PagePrint.render_to_file( '

Hello

', output_path, base_url: nil @@ -593,129 +593,129 @@ def test_html_to_pdf_accepts_nil_base_url end end - def test_html_to_pdf_requires_options_to_be_a_hash + def test_render_to_file_requires_options_to_be_a_hash error = assert_raises(TypeError) do - PagePrint.html_to_pdf('

Hello

', 'output.pdf', :options) + PagePrint.render_to_file('

Hello

', 'output.pdf', :options) end assert_equal 'options must be a Hash', error.message end - def test_html_to_pdf_requires_base_url_to_be_a_string_or_nil + def test_render_to_file_requires_base_url_to_be_a_string_or_nil error = assert_raises(TypeError) do - PagePrint.html_to_pdf('

Hello

', 'output.pdf', base_url: 123) + PagePrint.render_to_file('

Hello

', 'output.pdf', base_url: 123) end assert_equal 'base_url must be a String or nil', error.message end - def test_html_to_pdf_rejects_invalid_page_size + def test_render_to_file_rejects_invalid_page_size error = assert_raises(ArgumentError) do - PagePrint.html_to_pdf('

Hello

', 'output.pdf', page_size: :tabloid) + PagePrint.render_to_file('

Hello

', 'output.pdf', page_size: :tabloid) end assert_equal 'page_size must be one of: :a3, :a4, :a5, :b4, :b5, :letter, :legal, :ledger', error.message end - def test_html_to_pdf_requires_page_size_to_be_a_symbol_or_hash + def test_render_to_file_requires_page_size_to_be_a_symbol_or_hash error = assert_raises(TypeError) do - PagePrint.html_to_pdf('

Hello

', 'output.pdf', page_size: 'letter') + PagePrint.render_to_file('

Hello

', 'output.pdf', page_size: 'letter') end assert_equal 'page_size must be a Symbol or Hash', error.message end - def test_html_to_pdf_requires_custom_page_size_width + def test_render_to_file_requires_custom_page_size_width error = assert_raises(ArgumentError) do - PagePrint.html_to_pdf('

Hello

', 'output.pdf', page_size: { height: 150, unit: :mm }) + PagePrint.render_to_file('

Hello

', 'output.pdf', page_size: { height: 150, unit: :mm }) end assert_equal 'page_size requires :width', error.message end - def test_html_to_pdf_requires_custom_page_size_height + def test_render_to_file_requires_custom_page_size_height error = assert_raises(ArgumentError) do - PagePrint.html_to_pdf('

Hello

', 'output.pdf', page_size: { width: 100, unit: :mm }) + PagePrint.render_to_file('

Hello

', 'output.pdf', page_size: { width: 100, unit: :mm }) end assert_equal 'page_size requires :height', error.message end - def test_html_to_pdf_requires_custom_page_size_unit + def test_render_to_file_requires_custom_page_size_unit error = assert_raises(ArgumentError) do - PagePrint.html_to_pdf('

Hello

', 'output.pdf', page_size: { width: 100, height: 150 }) + PagePrint.render_to_file('

Hello

', 'output.pdf', page_size: { width: 100, height: 150 }) end assert_equal 'page_size requires :unit', error.message end - def test_html_to_pdf_rejects_invalid_custom_page_size_unit + def test_render_to_file_rejects_invalid_custom_page_size_unit error = assert_raises(ArgumentError) do - PagePrint.html_to_pdf('

Hello

', 'output.pdf', page_size: { width: 100, height: 150, unit: :meter }) + PagePrint.render_to_file('

Hello

', 'output.pdf', page_size: { width: 100, height: 150, unit: :meter }) end assert_equal 'page_size unit must be one of: :pt, :pc, :in, :cm, :mm, :px', error.message end - def test_html_to_pdf_rejects_non_positive_custom_page_size + def test_render_to_file_rejects_non_positive_custom_page_size error = assert_raises(ArgumentError) do - PagePrint.html_to_pdf('

Hello

', 'output.pdf', page_size: { width: 0, height: 150, unit: :mm }) + PagePrint.render_to_file('

Hello

', 'output.pdf', page_size: { width: 0, height: 150, unit: :mm }) end assert_equal 'page_size width must be greater than 0', error.message end - def test_html_to_pdf_rejects_invalid_margins + def test_render_to_file_rejects_invalid_margins error = assert_raises(ArgumentError) do - PagePrint.html_to_pdf('

Hello

', 'output.pdf', margins: :compact) + PagePrint.render_to_file('

Hello

', 'output.pdf', margins: :compact) end assert_equal 'margins must be one of: :none, :normal, :narrow, :moderate, :wide', error.message end - def test_html_to_pdf_requires_margins_to_be_a_symbol_or_hash + def test_render_to_file_requires_margins_to_be_a_symbol_or_hash error = assert_raises(TypeError) do - PagePrint.html_to_pdf('

Hello

', 'output.pdf', margins: 'narrow') + PagePrint.render_to_file('

Hello

', 'output.pdf', margins: 'narrow') end assert_equal 'margins must be a Symbol or Hash', error.message end - def test_html_to_pdf_requires_custom_margins_unit + def test_render_to_file_requires_custom_margins_unit error = assert_raises(ArgumentError) do - PagePrint.html_to_pdf('

Hello

', 'output.pdf', margins: { top: 1, right: 1, bottom: 1, left: 1 }) + PagePrint.render_to_file('

Hello

', 'output.pdf', margins: { top: 1, right: 1, bottom: 1, left: 1 }) end assert_equal 'margins requires :unit', error.message end - def test_html_to_pdf_rejects_negative_custom_margins + def test_render_to_file_rejects_negative_custom_margins error = assert_raises(ArgumentError) do - PagePrint.html_to_pdf('

Hello

', 'output.pdf', margins: { top: -1, right: 1, bottom: 1, left: 1, unit: :mm }) + PagePrint.render_to_file('

Hello

', 'output.pdf', margins: { top: -1, right: 1, bottom: 1, left: 1, unit: :mm }) end assert_equal 'margins values must be greater than or equal to 0', error.message end - def test_html_to_pdf_rejects_invalid_media + def test_render_to_file_rejects_invalid_media error = assert_raises(ArgumentError) do - PagePrint.html_to_pdf('

Hello

', 'output.pdf', media: :speech) + PagePrint.render_to_file('

Hello

', 'output.pdf', media: :speech) end assert_equal 'media must be one of: :print, :screen', error.message end - def test_html_to_pdf_requires_media_to_be_a_symbol + def test_render_to_file_requires_media_to_be_a_symbol error = assert_raises(TypeError) do - PagePrint.html_to_pdf('

Hello

', 'output.pdf', media: 'screen') + PagePrint.render_to_file('

Hello

', 'output.pdf', media: 'screen') end assert_equal 'media must be a Symbol', error.message end - def test_html_to_pdf_rejects_unknown_keyword + def test_render_to_file_rejects_unknown_keyword error = assert_raises(ArgumentError) do - PagePrint.html_to_pdf('

Hello

', 'output.pdf', foo: :bar) + PagePrint.render_to_file('

Hello

', 'output.pdf', foo: :bar) end assert_equal 'unknown keyword: :foo', error.message