-
Notifications
You must be signed in to change notification settings - Fork 2
/
PdfMemDocument.cc
138 lines (104 loc) · 4.01 KB
/
PdfMemDocument.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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
56
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
105
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
#include <node.h>
#include "PdfError.h"
#include "PdfInfo.h"
#include "PdfPage.h"
#include "PdfVecObjects.h"
#include "PdfMemDocument.h"
using v8::Handle;
using v8::Object;
using v8::Local;
using v8::FunctionTemplate;
using v8::Function;
using v8::String;
using v8::Number;
using v8::Boolean;
using v8::External;
using v8::Value;
Nan::Persistent<FunctionTemplate> PdfMemDocument::constructor_template;
PdfMemDocument::PdfMemDocument() {
_obj = new PoDoFo::PdfMemDocument();
}
PdfMemDocument::~PdfMemDocument() {
delete _obj;
}
void PdfMemDocument::Init(Handle<Object> exports) {
Nan::HandleScope scope;
// Prepare constructor template
Local<FunctionTemplate> tpl = Nan::New<FunctionTemplate>(New);
tpl->SetClassName(Nan::New("PdfMemDocument").ToLocalChecked());
tpl->InstanceTemplate()->SetInternalFieldCount(4);
// Prototype
Nan::SetPrototypeMethod(tpl, "Load", Load);
Nan::SetPrototypeMethod(tpl, "GetObjects", GetObjects);
Nan::SetPrototypeMethod(tpl, "GetInfo", GetInfo);
Nan::SetPrototypeMethod(tpl, "GetPageCount", GetPageCount);
Nan::SetPrototypeMethod(tpl, "GetPage", GetPage);
constructor_template.Reset(tpl);
exports->Set(Nan::New("PdfMemDocument").ToLocalChecked(), tpl->GetFunction());
}
NAN_METHOD(PdfMemDocument::New) {
Nan::HandleScope scope;
if (!info.IsConstructCall()) {
return Nan::ThrowTypeError("Use the new operator to create new PdfMemDocument objects");
}
PdfMemDocument* objects = new PdfMemDocument();
objects->Wrap(info.This());
info.GetReturnValue().Set(info.This());
}
NAN_METHOD(PdfMemDocument::Load) {
Nan::HandleScope scope;
if (info.Length() < 1) {
return Nan::ThrowTypeError("Load requires at least 1 argument");
}
PdfMemDocument* obj = ObjectWrap::Unwrap<PdfMemDocument>(info.This());
Local<String> filename = info[0]->ToString();
try {
Nan::Utf8String* charFilename = new Nan::Utf8String(filename);
obj->_obj->Load(charFilename->operator*());
} catch (PoDoFo::PdfError& e) {
Local<FunctionTemplate> errTpl = Nan::New(PdfError::constructor_template);
Local<Function> errFunc = errTpl->GetFunction();
Handle<Value> errFuncArgs[] = { Nan::New<External>(&e) };
Local<Value> errObj = errFunc->NewInstance(1, errFuncArgs);
Nan::ThrowError(errObj);
}
info.GetReturnValue().Set(Nan::Undefined());
}
NAN_METHOD(PdfMemDocument::GetObjects) {
Nan::HandleScope scope;
PdfMemDocument* obj = ObjectWrap::Unwrap<PdfMemDocument>(info.This());
Local<FunctionTemplate> tpl = Nan::New(PdfVecObjects::constructor_template);
Local<Function> func = tpl->GetFunction();
Handle<Value> newFuncArgs[] = { Nan::New<External>(&obj->_obj->GetObjects()) };
Local<Object> newObj = func->NewInstance(1, newFuncArgs);
info.GetReturnValue().Set(newObj);
}
NAN_METHOD(PdfMemDocument::GetInfo) {
Nan::HandleScope scope;
PdfMemDocument* obj = ObjectWrap::Unwrap<PdfMemDocument>(info.This());
Local<FunctionTemplate> tpl = Nan::New(PdfInfo::constructor_template);
Local<Function> func = tpl->GetFunction();
Handle<Value> newFuncArgs[] = { Nan::New<External>(obj->_obj->GetInfo()) };
Local<Object> newObj = func->NewInstance(1, newFuncArgs);
info.GetReturnValue().Set(newObj);
}
NAN_METHOD(PdfMemDocument::GetPageCount) {
Nan::HandleScope scope;
PdfMemDocument* obj = ObjectWrap::Unwrap<PdfMemDocument>(info.This());
int count = obj->_obj->GetPageCount();
info.GetReturnValue().Set(Nan::New<Number>(count));
}
NAN_METHOD(PdfMemDocument::GetPage) {
Nan::HandleScope scope;
if (info.Length() < 1) {
return Nan::ThrowTypeError("GetPage requires at least 1 argument");
}
PdfMemDocument* obj = ObjectWrap::Unwrap<PdfMemDocument>(info.This());
Local<Number> index = info[0]->ToNumber();
Local<FunctionTemplate> tpl = Nan::New(PdfPage::constructor_template);
Local<Function> func = tpl->GetFunction();
PoDoFo::PdfPage* page = obj->_obj->GetPage(index->Int32Value());
Handle<Value> newFuncArgs[] = { Nan::New<External>(page) };
Local<Object> newObj = func->NewInstance(1, newFuncArgs);
info.GetReturnValue().Set(newObj);
}