Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions .github/workflows/package.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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(\"<html><body><h1>Hello</h1></body></html>\"); 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(\"<html><body><h1>Hello</h1></body></html>\"); 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(\"<html><body><h1>Hello</h1></body></html>\"); 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(\"<html><body><h1>Hello</h1></body></html>\"); 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(\"<html><body><h1>Hello</h1></body></html>\"); 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(\"<html><body><h1>Hello</h1></body></html>\"); abort \"bad pdf\" unless pdf.start_with?(\"%PDF\"); puts \"ok\""'

- uses: actions/upload-artifact@v4
with:
Expand Down Expand Up @@ -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("<html><body><h1>Hello</h1></body></html>"); abort "bad pdf" unless pdf.start_with?("%PDF"); puts "ok"'
ruby -e 'require "page_print"; pdf = PagePrint.render("<html><body><h1>Hello</h1></body></html>"); abort "bad pdf" unless pdf.start_with?("%PDF"); puts "ok"'

- uses: actions/upload-artifact@v4
with:
Expand Down
4 changes: 2 additions & 2 deletions AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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`.
Expand Down
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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:
Expand Down Expand Up @@ -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 }
Expand Down Expand Up @@ -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.

Expand Down Expand Up @@ -213,13 +213,13 @@ require "tmpdir"

output_path = File.join(Dir.tmpdir, "page_print-output.pdf")

PagePrint.html_to_pdf("<html><body><h1>Hello</h1></body></html>", output_path, page_size: :letter, margins: :narrow, media: :screen)
PagePrint.render_to_file("<html><body><h1>Hello</h1></body></html>", 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("<html><body><h1>Hello</h1></body></html>", 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("<html><body><h1>Hello</h1></body></html>", 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:
Expand Down
2 changes: 1 addition & 1 deletion benchmark/pdf_renderers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 4 additions & 4 deletions ext/page_print/page_print.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Loading
Loading