Class: QpdfRuby::Document

Inherits:
Object
  • Object
show all
Defined in:
ext/qpdf_ruby/qpdf_ruby.cpp

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Object



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'ext/qpdf_ruby/qpdf_ruby.cpp', line 148

static VALUE doc_initialize(int argc, VALUE* argv, VALUE self) {
  VALUE filename, password;

  filename = Qnil;
  password = Qnil;

  rb_scan_args(argc, argv, "11", &filename, &password);  // 1 required, 1 optional

  Check_Type(filename, T_STRING);

  const char* pw = "";
  if (!NIL_P(password)) {
    Check_Type(password, T_STRING);
    pw = StringValueCStr(password);
  }

  try {
    DocumentHandle* h = qpdf_ruby::qpdf_ruby_open(StringValueCStr(filename), pw);
    if (!h) rb_sys_fail("qpdf_ruby_open");
    DATA_PTR(self) = h;
  } catch (const std::exception& e) {
    rb_raise(rb_eQpdfRubyError, "%s", e.what());
  }

  return self;
}

Class Method Details

.from_memory(str, password) ⇒ Object



196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'ext/qpdf_ruby/qpdf_ruby.cpp', line 196

static VALUE doc_from_memory(VALUE klass, VALUE str, VALUE password) {
  Check_Type(str, T_STRING);
  Check_Type(password, T_STRING);
  DocumentHandle* h;
  try {
    h = qpdf_ruby_open_memory("ruby-memory", reinterpret_cast<unsigned char const*>(RSTRING_PTR(str)), RSTRING_LEN(str),
                              StringValueCStr(password));
  } catch (const std::exception& e) {
    rb_raise(rb_eQpdfRubyError, "%s", e.what());
  }

  if (!h) rb_sys_fail("qpdf_ruby_open_memory");

  VALUE obj = Data_Wrap_Struct(klass, 0, doc_free, h);
  return obj;
}

Instance Method Details

#encrypt(*args) ⇒ Object

returns a Ruby ::String



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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
# File 'ext/qpdf_ruby/qpdf_ruby.cpp', line 219

VALUE rb_qpdf_doc_set_encryption(int argc, VALUE* argv, VALUE self) {
  VALUE kwargs;
  ID keys[12];
  VALUE values[12] = {
      rb_str_new_cstr(""),  // user_pw
      rb_str_new_cstr(""),  // owner_pw
      INT2NUM(4),           // encryption_revision
      INT2NUM(1),           // allow_print
      Qfalse,               // allow_modify
      Qfalse,               // allow_extract
      Qtrue,                // accessibility
      Qfalse,               // assemble
      Qfalse,               // annotate_and_form
      Qfalse,               // form_filling
      Qtrue,                // encrypt_metadata
      Qtrue                 // use_aes
  };

  keys[0] = rb_intern("user_pw");
  keys[1] = rb_intern("owner_pw");
  keys[2] = rb_intern("encryption_revision");
  keys[3] = rb_intern("allow_print");
  keys[4] = rb_intern("allow_modify");
  keys[5] = rb_intern("allow_extract");
  keys[6] = rb_intern("accessibility");
  keys[7] = rb_intern("assemble");
  keys[8] = rb_intern("annotate_and_form");
  keys[9] = rb_intern("form_filling");
  keys[10] = rb_intern("encrypt_metadata");
  keys[11] = rb_intern("use_aes");

  rb_scan_args(argc, argv, ":", &kwargs);

  // Get values for each keyword, or Qnil if missing
  rb_get_kwargs(kwargs, keys, 0, 12, values);

  DocumentHandle* h;
  Data_Get_Struct(self, DocumentHandle, h);

  h->set_encryption(StringValueCStr(values[0]),                        // user_pw
                    StringValueCStr(values[1]),                        // owner_pw
                    NUM2INT(values[2]),                                // r
                    static_cast<qpdf_r3_print_e>(NUM2INT(values[3])),  // allow_print
                    RTEST(values[4]),                                  // allow_modify
                    RTEST(values[5]),                                  // allow_extract
                    RTEST(values[6]),                                  // accessibility
                    RTEST(values[7]),                                  // assemble
                    RTEST(values[8]),                                  // annotate_and_form
                    RTEST(values[9]),                                  // form_filling
                    RTEST(values[10]),                                 // encrypt_metadata
                    RTEST(values[11])                                  // use_aes
  );
  return Qnil;
}

#ensure_bboxObject



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'ext/qpdf_ruby/qpdf_ruby.cpp', line 106

VALUE rb_qpdf_ensure_bboxs(VALUE self) {
  DocumentHandle* h;
  Data_Get_Struct(self, DocumentHandle, h);

  try {
    QPDF& pdf = h->qpdf();

    QPDFObjectHandle catalog = pdf.getRoot();
    QPDFObjectHandle struct_root = catalog.getKey("/StructTreeRoot");
    if (!struct_root.isDictionary()) {
      rb_raise(rb_eRuntimeError, "No StructTreeRoot found");
    }
    QPDFObjectHandle topKids = struct_root.getKey("/K");

    PDFImageMapper finder(0);

    finder.find(pdf);

    std::unordered_map<int, std::array<double, 4>> mcid2bbox;
    for (const auto& kv : finder.getImageMap()) {
      if (kv.second.mcid >= 0) mcid2bbox[kv.second.mcid] = kv.second.bbox;
    }

    PDFStructWalker walker(std::cout, mcid2bbox);  // For now, std::cout, unless you pass another stream

    if (topKids.isArray()) {
      for (int i = 0; i < topKids.getArrayNItems(); ++i) {
        walker.ensureLayoutBBox(topKids.getArrayItem(i));
      }
    } else {
      walker.ensureLayoutBBox(topKids);
    }
  } catch (const std::exception& e) {
    rb_raise(rb_eRuntimeError, "Error: %s", e.what());
  }
  return Qnil;
}

#mark_paths_as_artifactsObject



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'ext/qpdf_ruby/qpdf_ruby.cpp', line 57

VALUE rb_qpdf_mark_paths_as_artifacts(VALUE self) {
  DocumentHandle* h;
  Data_Get_Struct(self, DocumentHandle, h);
  try {
    QPDF& pdf = h->qpdf();

    std::vector<QPDFObjectHandle> pages = pdf.getAllPages();
    std::regex path_regex(R"((?:[-+]?\d*\.?\d+(?:e[-+]?\d+)?\s+){4}re\s+(?:S|s|f\*?|F|B\*?|b\*?|n))",
                          std::regex::ECMAScript | std::regex::optimize);

    for (auto& page_obj : pages) {
      QPDFPageObjectHelper poh(page_obj);
      std::vector<QPDFObjectHandle> contents = poh.getPageContents();
      std::vector<QPDFObjectHandle> new_contents_array;

      for (auto& content_stream : contents) {
        if (content_stream.isStream()) {
          // Use std::shared_ptr instead of PointerHolder
          std::shared_ptr<Buffer> stream_buffer = content_stream.getStreamData();
          std::string stream_data_str(reinterpret_cast<char*>(stream_buffer->getBuffer()), stream_buffer->getSize());

          std::string new_stream_data_str = std::regex_replace(stream_data_str, path_regex, "/Artifact BMC\n$&\nEMC");

          // Create a new Buffer with std::shared_ptr
          std::shared_ptr<Buffer> new_buffer = std::make_shared<Buffer>(new_stream_data_str.length());
          memcpy(new_buffer->getBuffer(), new_stream_data_str.data(), new_stream_data_str.length());

          QPDFObjectHandle new_stream = QPDFObjectHandle::newStream(&pdf, new_buffer);
          new_contents_array.push_back(new_stream);
        } else {
          new_contents_array.push_back(content_stream);
        }
      }

      if (new_contents_array.size() == 1) {
        page_obj.replaceKey("/Contents", new_contents_array[0]);
      } else {
        page_obj.replaceKey("/Contents", QPDFObjectHandle::newArray(new_contents_array));
      }
    }
  } catch (const QPDFExc& e) {  // Catching specific QPDF exceptions is good
    rb_raise(rb_eRuntimeError, "QPDF Error: %s (filename: %s)", e.what(), e.getFilename().c_str());
  } catch (const std::exception& e) {  // Fallback for other standard exceptions
    rb_raise(rb_eRuntimeError, "Error: %s", e.what());
  }

  return Qnil;
}

#show_structureObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'ext/qpdf_ruby/qpdf_ruby.cpp', line 24

VALUE rb_qpdf_get_structure_string(VALUE self) {
  DocumentHandle* h;
  Data_Get_Struct(self, DocumentHandle, h);

  try {
    QPDF& pdf = h->qpdf();

    QPDFObjectHandle catalog = pdf.getRoot();
    QPDFObjectHandle struct_root = catalog.getKey("/StructTreeRoot");
    if (!struct_root.isDictionary()) {
      rb_raise(rb_eRuntimeError, "No StructTreeRoot found");
    }
    QPDFObjectHandle topKids = struct_root.getKey("/K");

    PDFStructWalker walker(std::cout);  // For now, std::cout, unless you pass another stream
    walker.buildPageObjectMap(pdf);

    std::string result;
    if (topKids.isArray()) {
      for (int i = 0; i < topKids.getArrayNItems(); ++i) {
        result += walker.get_structure_as_string(topKids.getArrayItem(i));
      }
    } else {
      result = walker.get_structure_as_string(topKids);
    }

    return rb_str_new(result.c_str(), result.length());
  } catch (const std::exception& e) {
    rb_raise(rb_eRuntimeError, "Error: %s", e.what());
  }
  return Qnil;
}

#to_memoryObject



213
214
215
216
217
# File 'ext/qpdf_ruby/qpdf_ruby.cpp', line 213

static VALUE doc_to_memory(VALUE self) {
  DocumentHandle* h;
  Data_Get_Struct(self, DocumentHandle, h);
  return qpdf_ruby_write_memory(h);  // returns a Ruby ::String
}

#write(out_filename) ⇒ Object



175
176
177
178
179
180
181
182
# File 'ext/qpdf_ruby/qpdf_ruby.cpp', line 175

static VALUE doc_write(VALUE self, VALUE out_filename) {
  Check_Type(out_filename, T_STRING);
  DocumentHandle* h;
  Data_Get_Struct(self, DocumentHandle, h);
  if (qpdf_ruby::qpdf_ruby_write(h, StringValueCStr(out_filename)) == -1) rb_sys_fail("qpdf_ruby_write");

  return Qnil;
}