Class: PDFium::Document

Inherits:
Object
  • Object
show all
Defined in:
ext/pdfium_ext/document.cc

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.from_memory(pdf_data) ⇒ Document

Initializes a document from a binary string.

See Image#data for an example of reading a PDF directly from Amazon S3 and writing it’s images completely in memory.

Returns:



74
75
76
77
78
79
80
# File 'ext/pdfium_ext/document.cc', line 74

static VALUE
document_from_memory(VALUE klass, VALUE data){
    DocumentWrapper *doc = new DocumentWrapper();
    VALUE instance = Data_Wrap_Struct(klass, NULL, document_gc_free, doc );
    doc->document = (CPDF_Document*)FPDF_LoadMemDocument(RSTRING_PTR(data), RSTRING_LEN(data),NULL);
    return instance;
}

Instance Method Details

#bookmarksPDFium::BookmarkList

Retrieves the first Bookmark for a document

Returns:

  • (PDFium::BookmarkList)


149
150
151
152
153
154
155
156
157
158
# File 'ext/pdfium_ext/document.cc', line 149

static VALUE
document_bookmarks(VALUE self)
{
    VALUE args[1];
    args[0] = rb_hash_new();
    rb_hash_aset(args[0], ID2SYM(rb_intern("document")), self);
    VALUE bm = rb_class_new_instance( 1, args, RB::Bookmark() );
    args[0] = bm;
    return rb_class_new_instance( 1, args, RB::BookmarkList() );
}

#metadataHash

Retrieves and optionally sets the metadata on a document. Returns a hash with the following keys:

:title, :author :subject, :keywords, :creator, :producer, :creation_date, :mod_date

An empty Hash will be returned if the metadata cannot be read

All values in the hash are encoded as UTF-16LE strings.

If caled with a block, the values will be passed to it and updates written back to the Document

Example

pdf = PDFium::Document.new( "test.pdf" )
pdf. do | md |
    md[:title]  = "My Awesome PDF"
    md[:author] = "Nathan Stitt"
end
pdf.[:author] # => "Nathan Stitt"

Returns:

  • (Hash)


205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
# File 'ext/pdfium_ext/document.cc', line 205

VALUE
document_metadata(int argc, VALUE *argv, VALUE self)
{
    auto doc = RB2DOC(self);
    VALUE metadata = rb_hash_new();
    CPDF_Dictionary* info = doc->GetInfo();
    if (!info)
        return metadata;

    VALUE block;
    rb_scan_args(argc, argv, "0&", &block);

    std::map<std::string, std::string> keys = {
        { "Title",        "title"  },
        { "Author",       "author" },
        { "Subject",      "subject" },
        { "Keywords",     "keywords"},
        { "Creator",      "creator" },
        { "Producer",     "producer"},
        { "CreationDate", "creation_date" },
        { "ModDate",      "mod_date" }
    };

    for (auto& kv : keys) {
        rb_hash_aset(metadata,
                     ID2SYM( rb_intern( kv.second.c_str() ) ),
                     RB::to_string( info->GetUnicodeText( kv.first.c_str() ) )
                     );
    }

    if (RTEST(block)){
        rb_yield( metadata );
        for (auto& kv : keys) {
            VALUE value = RB::get_option(metadata, kv.second);
            auto bs = CFX_ByteString( RSTRING_PTR(value), RSTRING_LEN(value) );
            info->SetAtString(kv.first.c_str(), bs);
        }
    }

    return metadata;
}

#page_countObject

page_count -> Fixnum

Returns the number of pages on a Document



88
89
90
91
92
# File 'ext/pdfium_ext/document.cc', line 88

static VALUE
document_page_count(VALUE self)
{
    return INT2NUM( RB2DOC(self)->GetPageCount() );
}

#pagesPDFium::PageList

Returns a collection of all the pages on the document as a PDFium::PageList. Pages are lazily loaded.

Returns:

  • (PDFium::PageList)


113
114
115
116
117
118
119
# File 'ext/pdfium_ext/document.cc', line 113

static VALUE
document_pages(VALUE self)
{
    VALUE args[1];
    args[0] = self;
    return rb_class_new_instance( 1, args, RB::PageList() );
}

#save(file) ⇒ Boolean

Retrieves the first Bookmark for a document

Parameters:

  • file (String, Pathname)

    path to save the file to

Returns:

  • (Boolean)

    indicating success or failure



171
172
173
174
175
176
177
178
179
# File 'ext/pdfium_ext/document.cc', line 171

static VALUE
document_save(VALUE self, VALUE _path)
{
    auto doc = RB2DOC(self);
    VALUE path = RB::to_s(_path); // call to_s in case it's a Pathname
    BufferFileWrite output_file_write(StringValuePtr(path));
    FPDF_SaveAsCopy(doc, &output_file_write, FPDF_REMOVE_SECURITY);
    return self;
}