Module: PagePrint

Defined in:
lib/page_print.rb,
lib/page_print/railtie.rb,
lib/page_print/version.rb,
lib/page_print/rails_resource_fetcher.rb,
ext/page_print/page_print.c

Defined Under Namespace

Classes: RailsResourceFetcher, Railtie

Constant Summary collapse

VERSION =
'0.1.2'

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.base_urlObject



10
11
12
# File 'lib/page_print.rb', line 10

def base_url
  Thread.current[:page_print_base_url] || @base_url
end

.resource_fetcherObject

Returns the value of attribute resource_fetcher.



7
8
9
# File 'lib/page_print.rb', line 7

def resource_fetcher
  @resource_fetcher
end

Class Method Details

.configure {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:

  • _self (PagePrint)

    the object that the method was called on



14
15
16
# File 'lib/page_print.rb', line 14

def configure
  yield self
end

.html_to_pdf(*args) ⇒ Object



702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
# File 'ext/page_print/page_print.c', line 702

static VALUE pageprint_html_to_pdf(int argc, VALUE *argv, VALUE self) {
    VALUE html;
    VALUE path;
    VALUE options;

    const char *path_str;

    plutobook_t *book;
    pageprint_resource_fetcher_t resource_fetcher;
    pageprint_write_pdf_args_t write_args;

    rb_check_arity(argc, 2, 3);

    html = argv[0];
    path = argv[1];
    options = argc == 3 ? argv[2] : Qnil;

    if (!RB_TYPE_P(path, T_STRING)) {
        rb_raise(rb_eTypeError, "path must be a String");
    }

    if (RSTRING_LEN(path) == 0) {
        rb_raise(rb_eArgError, "path must not be empty");
    }

    path_str = StringValueCStr(path);

    book = pageprint_create_book_from_html(html, options, &resource_fetcher);

    plutobook_clear_error_message();

    write_args.book = book;
    write_args.path = path_str;
    write_args.ok = 0;

    rb_thread_call_without_gvl(
        pageprint_write_pdf_without_gvl,
        &write_args,
        RUBY_UBF_IO,
        NULL
    );

    plutobook_destroy(book);

    if (resource_fetcher.state) {
        rb_jump_tag(resource_fetcher.state);
    }

    if (!write_args.ok) {
        pageprint_raise_plutobook_error_with_path(rb_eRuntimeError, "failed to write PDF to", path_str);
    }

    RB_GC_GUARD(resource_fetcher.object);
    RB_GC_GUARD(options);
    RB_GC_GUARD(path);

    return Qtrue;
}

.html_to_pdf_string(*args) ⇒ Object



761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
# File 'ext/page_print/page_print.c', line 761

static VALUE pageprint_html_to_pdf_string(int argc, VALUE *argv, VALUE self) {
    VALUE html;
    VALUE options;
    pageprint_pdf_string_output_t output;

    plutobook_t *book;
    pageprint_resource_fetcher_t resource_fetcher;
    pageprint_write_pdf_stream_args_t write_args;

    rb_check_arity(argc, 1, 2);

    html = argv[0];
    options = argc == 2 ? argv[1] : Qnil;
    output.output = rb_str_new(NULL, 0);
    output.state = 0;
    rb_enc_associate_index(output.output, rb_ascii8bit_encindex());

    book = pageprint_create_book_from_html(html, options, &resource_fetcher);

    plutobook_clear_error_message();

    write_args.book = book;
    write_args.output = &output;
    write_args.ok = 0;

    rb_thread_call_without_gvl(
        pageprint_write_pdf_stream_without_gvl,
        &write_args,
        RUBY_UBF_IO,
        NULL
    );

    plutobook_destroy(book);

    if (resource_fetcher.state) {
        rb_jump_tag(resource_fetcher.state);
    }

    if (output.state) {
        rb_jump_tag(output.state);
    }

    if (!write_args.ok) {
        pageprint_raise_plutobook_error(rb_eRuntimeError, "failed to write PDF to string");
    }

    RB_GC_GUARD(resource_fetcher.object);
    RB_GC_GUARD(options);
    RB_GC_GUARD(output.output);

    return output.output;
}

.with_base_url(base_url) ⇒ Object



18
19
20
21
22
23
24
# File 'lib/page_print.rb', line 18

def with_base_url(base_url)
  previous_base_url = Thread.current[:page_print_base_url]
  Thread.current[:page_print_base_url] = base_url
  yield
ensure
  Thread.current[:page_print_base_url] = previous_base_url
end