UNPKG

31.6 kBtext/x-cView Raw
1/*
2 Source File : Hummus
3
4
5 Copyright 2013 Gal Kahana HummusJS
6
7 Licensed under the Apache License, Version 2.0 (the "License");
8 you may not use this file except in compliance with the License.
9 You may obtain a copy of the License at
10
11 http://www.apache.org/licenses/LICENSE-2.0
12
13 Unless required by applicable law or agreed to in writing, software
14 distributed under the License is distributed on an "AS IS" BASIS,
15 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 See the License for the specific language governing permissions and
17 limitations under the License.
18
19 */
20#include <node.h>
21#include <v8.h>
22
23#include "nodes.h"
24#include "PDFWriterDriver.h"
25#include "PDFPageDriver.h"
26#include "PageContentContextDriver.h"
27#include "FormXObjectDriver.h"
28#include "XObjectContentContextDriver.h"
29#include "ResourcesDictionaryDriver.h"
30#include "UsedFontDriver.h"
31#include "ImageXObjectDriver.h"
32#include "ObjectsContextDriver.h"
33#include "DocumentCopyingContextDriver.h"
34#include "PDFReaderDriver.h"
35#include "PDFArrayDriver.h"
36#include "PDFDictionaryDriver.h"
37#include "PDFStreamInputDriver.h"
38#include "PDFIndirectObjectReferenceDriver.h"
39#include "PDFBooleanDriver.h"
40#include "PDFLiteralStringDriver.h"
41#include "PDFHexStringDriver.h"
42#include "PDFNullDriver.h"
43#include "PDFNameDriver.h"
44#include "PDFIntegerDriver.h"
45#include "PDFRealDriver.h"
46#include "PDFSymbolDriver.h"
47#include "ProcsetResourcesConstants.h"
48#include "EPDFVersion.h"
49#include "PDFEmbedParameterTypes.h"
50#include "PDFObject.h"
51#include "PDFTextStringDriver.h"
52#include "PDFDateDriver.h"
53#include "ETokenSeparator.h"
54#include "DictionaryContextDriver.h"
55#include "PDFPageInputDriver.h"
56#include "InputFileDriver.h"
57#include "OutputFileDriver.h"
58#include "DocumentContextDriver.h"
59#include "InfoDictionaryDriver.h"
60#include "ObjectByteWriterWithPosition.h"
61#include "IByteReaderWithPosition.h"
62#include "ObjectByteReaderWithPosition.h"
63#include "ObjectByteWriter.h"
64#include "PDFStreamDriver.h"
65#include "ByteReaderDriver.h"
66#include "ByteWriterDriver.h"
67#include "ByteReaderWithPositionDriver.h"
68#include "ByteWriterWithPositionDriver.h"
69#include "PDFWriter.h"
70#include "PDFPageModifierDriver.h"
71#include "PDFObjectParserDriver.h"
72#include "ConstructorsHolder.h"
73
74using namespace v8;
75using namespace node;
76
77METHOD_RETURN_TYPE CreateWriter(const ARGS_TYPE& args)
78{
79 CREATE_ISOLATE_CONTEXT;
80 CREATE_ESCAPABLE_SCOPE;
81 EXPOSE_EXTERNAL_ARGS(ConstructorsHolder, holder)
82 Local<Value> instance = holder->GetNewPDFWriter(args);
83
84 PDFWriterDriver* driver = ObjectWrap::Unwrap<PDFWriterDriver>(instance->TO_OBJECT());
85
86 if (args.Length() < 1 || args.Length() > 2) {
87 THROW_EXCEPTION("Wrong number of arguments, Provide one argument stating the location of the output file, and an optional options object");
88 SET_FUNCTION_RETURN_VALUE(UNDEFINED)
89 }
90
91 if (!args[0]->IsString() && !args[0]->IsObject()) {
92 THROW_EXCEPTION("Wrong arguments, please provide a path to a file as the first argument or a stream object");
93 SET_FUNCTION_RETURN_VALUE(UNDEFINED)
94 }
95
96 EPDFVersion pdfVersion = ePDFVersion14;
97 PDFCreationSettings pdfCreationSettings(true,true);
98 LogConfiguration logConfig = LogConfiguration::DefaultLogConfiguration();
99
100 if(args.Length() == 2 && args[1]->IsObject())
101 {
102 Local<Object> anObject = args[1]->TO_OBJECT();
103 if(anObject->Has(GET_CURRENT_CONTEXT, NEW_STRING("version")).FromJust() && anObject->Get(GET_CURRENT_CONTEXT, NEW_STRING("version")).ToLocalChecked()->IsNumber())
104 {
105 long pdfVersionValue = TO_INT32(anObject->Get(GET_CURRENT_CONTEXT, NEW_STRING("version")).ToLocalChecked())->Value();
106
107 if(pdfVersionValue < ePDFVersion10 || ePDFVersionMax < pdfVersionValue)
108 {
109 THROW_EXCEPTION("Wrong argument for PDF version, please provide a valid PDF version");
110 SET_FUNCTION_RETURN_VALUE(UNDEFINED)
111 }
112 pdfVersion = (EPDFVersion)pdfVersionValue;
113 }
114
115 if(anObject->Has(GET_CURRENT_CONTEXT, NEW_STRING("compress")).FromJust() && anObject->Get(GET_CURRENT_CONTEXT, NEW_STRING("compress")).ToLocalChecked()->IsBoolean())
116 pdfCreationSettings.CompressStreams = anObject->Get(GET_CURRENT_CONTEXT, NEW_STRING("compress")).ToLocalChecked()->TO_BOOLEAN()->Value();
117
118 if(anObject->Has(GET_CURRENT_CONTEXT, NEW_STRING("log")).FromJust() && anObject->Get(GET_CURRENT_CONTEXT, NEW_STRING("log")).ToLocalChecked()->IsString())
119 {
120 logConfig.ShouldLog = true;
121 logConfig.LogFileLocation = *UTF_8_VALUE(anObject->Get(GET_CURRENT_CONTEXT, NEW_STRING("log")).ToLocalChecked()->TO_STRING());
122 }
123
124 if(anObject->Has(GET_CURRENT_CONTEXT, NEW_STRING("userPassword")).FromJust() && anObject->Get(GET_CURRENT_CONTEXT, NEW_STRING("userPassword")).ToLocalChecked()->IsString())
125 {
126 pdfCreationSettings.DocumentEncryptionOptions.ShouldEncrypt = true;
127 pdfCreationSettings.DocumentEncryptionOptions.UserPassword = *UTF_8_VALUE(anObject->Get(GET_CURRENT_CONTEXT, NEW_STRING("userPassword")).ToLocalChecked()->TO_STRING());
128 }
129
130 if(anObject->Has(GET_CURRENT_CONTEXT, NEW_STRING("ownerPassword")).FromJust() && anObject->Get(GET_CURRENT_CONTEXT, NEW_STRING("ownerPassword")).ToLocalChecked()->IsString())
131 {
132 pdfCreationSettings.DocumentEncryptionOptions.OwnerPassword = *UTF_8_VALUE(anObject->Get(GET_CURRENT_CONTEXT, NEW_STRING("ownerPassword")).ToLocalChecked()->TO_STRING());
133 }
134
135 if(anObject->Has(GET_CURRENT_CONTEXT, NEW_STRING("userProtectionFlag")).FromJust() && anObject->Get(GET_CURRENT_CONTEXT, NEW_STRING("userProtectionFlag")).ToLocalChecked()->IsNumber())
136 {
137 pdfCreationSettings.DocumentEncryptionOptions.UserProtectionOptionsFlag = TO_INT32(anObject->Get(GET_CURRENT_CONTEXT, NEW_STRING("userProtectionFlag")).ToLocalChecked())->Value();
138 }
139 else // default to print only
140 pdfCreationSettings.DocumentEncryptionOptions.UserProtectionOptionsFlag = 4;
141 }
142
143 EStatusCode status;
144
145 if(args[0]->IsObject())
146 {
147 status = driver->StartPDF(args[0]->TO_OBJECT(), pdfVersion,logConfig,pdfCreationSettings);
148 }
149 else
150 {
151 status = driver->StartPDF(std::string(*UTF_8_VALUE(args[0]->TO_STRING())), pdfVersion,logConfig,pdfCreationSettings);
152 }
153
154 if(status != PDFHummus::eSuccess)
155 {
156 THROW_EXCEPTION("Unable to create PDF file, make sure that output file target is available");
157 SET_FUNCTION_RETURN_VALUE(UNDEFINED)
158 }
159 SET_FUNCTION_RETURN_VALUE(instance)
160
161}
162
163/*
164 hummus.recrypt(
165 inOriginalPath/inOriginalStream,
166 inNewPath/inNewStream,
167 [options])
168
169 options =
170 {
171 password:
172 version:
173 compress:
174 log:
175 userPassword:
176 ownerPassword: // must pass userPassword!
177 userProtectionFlag: // must pass userPassword!
178 }
179*/
180
181METHOD_RETURN_TYPE Recrypt(const ARGS_TYPE& args)
182{
183 CREATE_ISOLATE_CONTEXT;
184 CREATE_ESCAPABLE_SCOPE;
185
186 if (args.Length() < 2 || args.Length() > 3) {
187 THROW_EXCEPTION("Wrong number of arguments, Provide one argument stating the location of the source file, a second one for the destination file, and an optional options object");
188 SET_FUNCTION_RETURN_VALUE(UNDEFINED)
189 }
190
191 if (!args[0]->IsString() && !args[0]->IsObject()) {
192 THROW_EXCEPTION("Wrong arguments, please provide a path to a file as the first argument or a stream object");
193 SET_FUNCTION_RETURN_VALUE(UNDEFINED)
194 }
195
196 if (!args[1]->IsString() && !args[1]->IsObject()) {
197 THROW_EXCEPTION("Wrong arguments, please provide a path to a file as the second argument or a stream object");
198 SET_FUNCTION_RETURN_VALUE(UNDEFINED)
199 }
200
201 if((args[1]->IsString() && !args[0]->IsString()) || (args[1]->IsObject() && !args[0]->IsObject())) {
202 THROW_EXCEPTION("Wrong arguments, please either provide two paths or two stream objects for the first two arguments");
203 SET_FUNCTION_RETURN_VALUE(UNDEFINED)
204 }
205
206
207 EPDFVersion pdfVersion = ePDFVersionUndefined;
208 PDFCreationSettings pdfCreationSettings(true,true);
209 LogConfiguration logConfig = LogConfiguration::DefaultLogConfiguration();
210 std::string originalPassword;
211
212 if(args.Length() == 3 && args[2]->IsObject())
213 {
214 Local<Object> anObject = args[2]->TO_OBJECT();
215 if(anObject->Has(GET_CURRENT_CONTEXT, NEW_STRING("version")).FromJust() && anObject->Get(GET_CURRENT_CONTEXT, NEW_STRING("version")).ToLocalChecked()->IsNumber())
216 {
217 long pdfVersionValue = TO_INT32(anObject->Get(GET_CURRENT_CONTEXT, NEW_STRING("version")).ToLocalChecked())->Value();
218
219 if(pdfVersionValue != ePDFVersionUndefined && (pdfVersionValue < ePDFVersion10 || ePDFVersionMax < pdfVersionValue))
220 {
221 THROW_EXCEPTION("Wrong argument for PDF version, please provide a valid PDF version");
222 SET_FUNCTION_RETURN_VALUE(UNDEFINED)
223 }
224 pdfVersion = (EPDFVersion)pdfVersionValue;
225 }
226
227 if(anObject->Has(GET_CURRENT_CONTEXT, NEW_STRING("compress")).FromJust() && anObject->Get(GET_CURRENT_CONTEXT, NEW_STRING("compress")).ToLocalChecked()->IsBoolean())
228 pdfCreationSettings.CompressStreams = anObject->Get(GET_CURRENT_CONTEXT, NEW_STRING("compress")).ToLocalChecked()->TO_BOOLEAN()->Value();
229
230 if(anObject->Has(GET_CURRENT_CONTEXT, NEW_STRING("log")).FromJust() && anObject->Get(GET_CURRENT_CONTEXT, NEW_STRING("log")).ToLocalChecked()->IsString())
231 {
232 logConfig.ShouldLog = true;
233 logConfig.LogFileLocation = *UTF_8_VALUE(anObject->Get(GET_CURRENT_CONTEXT, NEW_STRING("log")).ToLocalChecked()->TO_STRING());
234 }
235
236 if(anObject->Has(GET_CURRENT_CONTEXT, NEW_STRING("password")).FromJust() && anObject->Get(GET_CURRENT_CONTEXT, NEW_STRING("password")).ToLocalChecked()->IsString())
237 {
238 originalPassword = *UTF_8_VALUE(anObject->Get(GET_CURRENT_CONTEXT, NEW_STRING("password")).ToLocalChecked()->TO_STRING());
239 }
240
241 if(anObject->Has(GET_CURRENT_CONTEXT, NEW_STRING("userPassword")).FromJust() && anObject->Get(GET_CURRENT_CONTEXT, NEW_STRING("userPassword")).ToLocalChecked()->IsString())
242 {
243 pdfCreationSettings.DocumentEncryptionOptions.ShouldEncrypt = true;
244 pdfCreationSettings.DocumentEncryptionOptions.UserPassword = *UTF_8_VALUE(anObject->Get(GET_CURRENT_CONTEXT, NEW_STRING("userPassword")).ToLocalChecked()->TO_STRING());
245 }
246
247 if(anObject->Has(GET_CURRENT_CONTEXT, NEW_STRING("ownerPassword")).FromJust() && anObject->Get(GET_CURRENT_CONTEXT, NEW_STRING("ownerPassword")).ToLocalChecked()->IsString())
248 {
249 pdfCreationSettings.DocumentEncryptionOptions.OwnerPassword = *UTF_8_VALUE(anObject->Get(GET_CURRENT_CONTEXT, NEW_STRING("ownerPassword")).ToLocalChecked()->TO_STRING());
250 }
251
252 if(anObject->Has(GET_CURRENT_CONTEXT, NEW_STRING("userProtectionFlag")).FromJust() && anObject->Get(GET_CURRENT_CONTEXT, NEW_STRING("userProtectionFlag")).ToLocalChecked()->IsNumber())
253 {
254 pdfCreationSettings.DocumentEncryptionOptions.UserProtectionOptionsFlag = TO_INT32(anObject->Get(GET_CURRENT_CONTEXT, NEW_STRING("userProtectionFlag")).ToLocalChecked())->Value();
255 }
256 else // default to print only
257 pdfCreationSettings.DocumentEncryptionOptions.UserProtectionOptionsFlag = 4;
258 }
259
260 EStatusCode status;
261
262 if(args[0]->IsObject())
263 {
264 ObjectByteReaderWithPosition readStreamProxy(args[0]->TO_OBJECT());
265 ObjectByteWriterWithPosition writeStreamProxy(args[1]->TO_OBJECT());
266 status = PDFWriter::RecryptPDF(&readStreamProxy,
267 originalPassword,
268 &writeStreamProxy,
269 logConfig,
270 pdfCreationSettings,
271 pdfVersion);
272 }
273 else
274 {
275 status = PDFWriter::RecryptPDF(std::string(*UTF_8_VALUE(args[0]->TO_STRING())),
276 originalPassword,
277 std::string(*UTF_8_VALUE(args[1]->TO_STRING())),
278 logConfig,
279 pdfCreationSettings,
280 pdfVersion);
281 }
282
283 if(status != PDFHummus::eSuccess)
284 {
285 THROW_EXCEPTION("Unable to recrypt files, check that input and output files are clear and arguments are coool");
286 SET_FUNCTION_RETURN_VALUE(UNDEFINED)
287 }
288 SET_FUNCTION_RETURN_VALUE(UNDEFINED)
289
290}
291
292METHOD_RETURN_TYPE CreateWriterToContinue(const ARGS_TYPE& args)
293{
294 CREATE_ISOLATE_CONTEXT;
295 CREATE_ESCAPABLE_SCOPE;
296 EXPOSE_EXTERNAL_ARGS(ConstructorsHolder, holder)
297 Local<Value> instance = holder->GetNewPDFWriter(args);
298
299 PDFWriterDriver* driver = ObjectWrap::Unwrap<PDFWriterDriver>(instance->TO_OBJECT());
300
301 if ((args.Length() != 2 && args.Length() !=3)||
302 (!args[0]->IsString() && !args[0]->IsObject()) ||
303 !args[1]->IsString() ||
304 ((args.Length() == 3) && !args[2]->IsObject())) {
305 THROW_EXCEPTION("Wrong arguments, provide 2 strings - path to file to continue, and path to state file (provided to the previous shutdown call. You may also add an options object");
306 SET_FUNCTION_RETURN_VALUE(UNDEFINED)
307 }
308
309 std::string alternativePath;
310 Local<Object> alternativeStream;
311 LogConfiguration logConfig = LogConfiguration::DefaultLogConfiguration();
312
313 if(args.Length() == 2 && args[1]->IsObject())
314 {
315 Local<Object> anObject = args[1]->TO_OBJECT();
316
317 if(anObject->Has(GET_CURRENT_CONTEXT, NEW_STRING("modifiedFilePath")).FromJust() && anObject->Get(GET_CURRENT_CONTEXT, NEW_STRING("modifiedFilePath")).ToLocalChecked()->IsString())
318 alternativePath = *UTF_8_VALUE(anObject->Get(GET_CURRENT_CONTEXT, NEW_STRING("modifiedFilePath")).ToLocalChecked()->TO_STRING());
319
320 if(anObject->Has(GET_CURRENT_CONTEXT, NEW_STRING("modifiedStream")).FromJust() && anObject->Get(GET_CURRENT_CONTEXT, NEW_STRING("modifiedStream")).ToLocalChecked()->IsObject())
321 alternativeStream = anObject->Get(GET_CURRENT_CONTEXT, NEW_STRING("modifiedStream")).ToLocalChecked()->TO_OBJECT();
322
323
324 if(anObject->Has(GET_CURRENT_CONTEXT, NEW_STRING("log")).FromJust())
325 {
326 Local<Value> value = anObject->Get(GET_CURRENT_CONTEXT, NEW_STRING("log")).ToLocalChecked();
327 if(value->IsString())
328 {
329 logConfig.ShouldLog = true;
330 logConfig.LogFileLocation = *UTF_8_VALUE(anObject->Get(GET_CURRENT_CONTEXT, NEW_STRING("log")).ToLocalChecked()->TO_STRING());
331 logConfig.LogStream = NULL;
332 }
333 else if(value->IsObject())
334 {
335 logConfig.ShouldLog = true;
336 logConfig.LogFileLocation = "";
337 ObjectByteWriter proxy(value->TO_OBJECT());
338 logConfig.LogStream = &proxy;
339 }
340 }
341 }
342
343 EStatusCode status;
344
345 if(args[0]->IsObject())
346 {
347 status = driver->ContinuePDF(args[0]->TO_OBJECT(),
348 *UTF_8_VALUE(args[1]->TO_STRING()),
349 alternativeStream,
350 logConfig);
351 }
352 else
353 {
354 status = driver->ContinuePDF(*UTF_8_VALUE(args[0]->TO_STRING()),
355 *UTF_8_VALUE(args[1]->TO_STRING()),
356 alternativePath,
357 logConfig);
358 }
359
360 if(status != PDFHummus::eSuccess)
361 {
362 THROW_EXCEPTION("Unable to continue PDF file, make sure that output file target is available and state file exists");
363 SET_FUNCTION_RETURN_VALUE(UNDEFINED)
364 }
365 SET_FUNCTION_RETURN_VALUE(instance)
366}
367
368
369METHOD_RETURN_TYPE CreateWriterToModify(const ARGS_TYPE& args)
370{
371 CREATE_ISOLATE_CONTEXT;
372 CREATE_ESCAPABLE_SCOPE;
373 EXPOSE_EXTERNAL_ARGS(ConstructorsHolder, holder)
374 Local<Value> instance = holder->GetNewPDFWriter(args);
375
376 PDFWriterDriver* driver = ObjectWrap::Unwrap<PDFWriterDriver>(instance->TO_OBJECT());
377
378 if(args.Length() < 1 ||
379 (!args[0]->IsString() && !args[0]->IsObject()) ||
380 (args[0]->IsString() && args.Length() > 2) ||
381 (args[0]->IsObject() && (!args[1]->IsObject() || args.Length() > 3)))
382 {
383 THROW_EXCEPTION("Wrong arguments, please path a path to modified file, or a pair of stream - first for the source, and second for destination. in addition you can optionally add an options object");
384 SET_FUNCTION_RETURN_VALUE(UNDEFINED)
385 }
386
387
388 EPDFVersion pdfVersion = ePDFVersion10;
389 std::string alternativePath;
390 Local<Value> alternativeStream;
391 PDFCreationSettings pdfCreationSettings(true,true);
392 LogConfiguration logConfig = LogConfiguration::DefaultLogConfiguration();
393
394 int optionsObjectIndex = args[0]->IsString() ? 1:2;
395
396 if(args.Length() == (optionsObjectIndex+1) && args[optionsObjectIndex]->IsObject())
397 {
398 Local<Object> anObject = args[optionsObjectIndex]->TO_OBJECT();
399 if(anObject->Has(GET_CURRENT_CONTEXT, NEW_STRING("version")).FromJust() && anObject->Get(GET_CURRENT_CONTEXT, NEW_STRING("version")).ToLocalChecked()->IsString())
400 {
401 long pdfVersionValue = TO_INT32(anObject->Get(GET_CURRENT_CONTEXT, NEW_STRING("version")).ToLocalChecked())->Value();
402
403 if(pdfVersionValue < ePDFVersion10 || ePDFVersionMax < pdfVersionValue)
404 {
405 THROW_EXCEPTION("Wrong argument for PDF version, please provide a valid PDF version");
406 SET_FUNCTION_RETURN_VALUE(UNDEFINED)
407 }
408 pdfVersion = (EPDFVersion)pdfVersionValue;
409 }
410
411 if(anObject->Has(GET_CURRENT_CONTEXT, NEW_STRING("compress")).FromJust() && anObject->Get(GET_CURRENT_CONTEXT, NEW_STRING("compress")).ToLocalChecked()->IsBoolean())
412 pdfCreationSettings.CompressStreams = anObject->Get(GET_CURRENT_CONTEXT, NEW_STRING("compress")).ToLocalChecked()->TO_BOOLEAN()->Value();
413
414 if(anObject->Has(GET_CURRENT_CONTEXT, NEW_STRING("modifiedFilePath")).FromJust() && anObject->Get(GET_CURRENT_CONTEXT, NEW_STRING("modifiedFilePath")).ToLocalChecked()->IsString())
415 alternativePath = *UTF_8_VALUE(anObject->Get(GET_CURRENT_CONTEXT, NEW_STRING("modifiedFilePath")).ToLocalChecked()->TO_STRING());
416
417 if(anObject->Has(GET_CURRENT_CONTEXT, NEW_STRING("log")).FromJust() && anObject->Get(GET_CURRENT_CONTEXT, NEW_STRING("log")).ToLocalChecked()->IsString())
418 {
419 logConfig.ShouldLog = true;
420 logConfig.LogFileLocation = *UTF_8_VALUE(anObject->Get(GET_CURRENT_CONTEXT, NEW_STRING("log")).ToLocalChecked()->TO_STRING());
421 }
422
423 if(anObject->Has(GET_CURRENT_CONTEXT, NEW_STRING("userPassword")).FromJust() && anObject->Get(GET_CURRENT_CONTEXT, NEW_STRING("userPassword")).ToLocalChecked()->IsString())
424 {
425 pdfCreationSettings.DocumentEncryptionOptions.ShouldEncrypt = true;
426 pdfCreationSettings.DocumentEncryptionOptions.UserPassword = *UTF_8_VALUE(anObject->Get(GET_CURRENT_CONTEXT, NEW_STRING("userPassword")).ToLocalChecked()->TO_STRING());
427 }
428
429 if(anObject->Has(GET_CURRENT_CONTEXT, NEW_STRING("ownerPassword")).FromJust() && anObject->Get(GET_CURRENT_CONTEXT, NEW_STRING("ownerPassword")).ToLocalChecked()->IsString())
430 {
431 pdfCreationSettings.DocumentEncryptionOptions.OwnerPassword = *UTF_8_VALUE(anObject->Get(GET_CURRENT_CONTEXT, NEW_STRING("ownerPassword")).ToLocalChecked()->TO_STRING());
432 }
433
434 if(anObject->Has(GET_CURRENT_CONTEXT, NEW_STRING("userProtectionFlag")).FromJust() && anObject->Get(GET_CURRENT_CONTEXT, NEW_STRING("userProtectionFlag")).ToLocalChecked()->IsNumber())
435 {
436 pdfCreationSettings.DocumentEncryptionOptions.UserProtectionOptionsFlag = TO_INT32(anObject->Get(GET_CURRENT_CONTEXT, NEW_STRING("userProtectionFlag")).ToLocalChecked())->Value();
437 }
438 else // default to print only
439 pdfCreationSettings.DocumentEncryptionOptions.UserProtectionOptionsFlag = 4;
440 }
441
442 EStatusCode status;
443
444 if(args[0]->IsObject())
445 {
446 status = driver->ModifyPDF(args[0]->TO_OBJECT(),
447 args[1]->TO_OBJECT(),
448 pdfVersion,
449 logConfig,
450 pdfCreationSettings);
451 }
452 else
453 {
454 status = driver->ModifyPDF(*UTF_8_VALUE(args[0]->TO_STRING()),
455 pdfVersion,alternativePath,logConfig,
456 pdfCreationSettings);
457 }
458
459 if(status != PDFHummus::eSuccess)
460 {
461 THROW_EXCEPTION("Unable to modify PDF file, make sure that output file target is available and that it is not protected");
462 SET_FUNCTION_RETURN_VALUE(UNDEFINED)
463 }
464 SET_FUNCTION_RETURN_VALUE(instance)
465}
466
467
468METHOD_RETURN_TYPE CreateReader(const ARGS_TYPE& args)
469{
470 CREATE_ISOLATE_CONTEXT;
471 CREATE_ESCAPABLE_SCOPE;
472 EXPOSE_EXTERNAL_ARGS(ConstructorsHolder, holder)
473 Local<Value> instance = holder->GetNewPDFReader(args);
474
475 PDFReaderDriver* driver = ObjectWrap::Unwrap<PDFReaderDriver>(instance->TO_OBJECT());
476
477 if (args.Length() < 1 ||
478 args.Length() > 2 ||
479 (!args[0]->IsString() && !args[0]->IsObject()) ||
480 (args.Length() >=2 && !args[1]->IsObject()))
481 {
482 THROW_EXCEPTION("Wrong arguments, provide 1 string - path to file read, or a read stream object, and optionally an options object");
483 SET_FUNCTION_RETURN_VALUE(UNDEFINED)
484 }
485
486 PDFHummus::EStatusCode status;
487 PDFParsingOptions parsingOptions;
488
489 if(args.Length() >= 2)
490 {
491 Local<Object> options = args[1]->TO_OBJECT();
492 if(options->Has(GET_CURRENT_CONTEXT, NEW_STRING("password")).FromJust() && options->Get(GET_CURRENT_CONTEXT, NEW_STRING("password")).ToLocalChecked()->IsString())
493 {
494 parsingOptions.Password = *UTF_8_VALUE(options->Get(GET_CURRENT_CONTEXT, NEW_STRING("password")).ToLocalChecked()->TO_STRING());
495 }
496 }
497
498
499 if(args[0]->IsObject())
500 status = driver->StartPDFParsing(args[0]->TO_OBJECT(),parsingOptions);
501 else
502
503 status = driver->StartPDFParsing(std::string(*UTF_8_VALUE(args[0]->TO_STRING())),parsingOptions);
504 if(status != PDFHummus::eSuccess)
505 {
506 THROW_EXCEPTION("Unable to start parsing PDF file");
507 SET_FUNCTION_RETURN_VALUE(UNDEFINED)
508 }
509 SET_FUNCTION_RETURN_VALUE(instance)
510}
511
512METHOD_RETURN_TYPE GetTypeLabel(const ARGS_TYPE& args)
513{
514 CREATE_ISOLATE_CONTEXT;
515 CREATE_ESCAPABLE_SCOPE;
516 if (args.Length() != 1 || !args[0]->IsNumber())
517 {
518 THROW_EXCEPTION("Wrong arguments, provide a single enumerator value of a PDF Object type");
519 SET_FUNCTION_RETURN_VALUE(UNDEFINED)
520 }
521
522 unsigned long value = TO_NUMBER(args[0])->Value();
523
524 if(value > PDFObject::ePDFObjectSymbol)
525 {
526 THROW_EXCEPTION("Wrong arguments, provide a single enumerator value of a PDF Object type");
527 SET_FUNCTION_RETURN_VALUE(UNDEFINED)
528 }
529
530 Local<Value> result = NEW_STRING(PDFObject::scPDFObjectTypeLabel((PDFObject::EPDFObjectType)value));
531
532 SET_FUNCTION_RETURN_VALUE(result)
533}
534
535DEF_INIT(HummusInit) {
536 CREATE_ISOLATE_CONTEXT;
537
538 DECLARE_EXTERNAL(ConstructorsHolder)
539
540 CALL_INIT_WITH_EXPORTS(PDFWriterDriver::Init)
541 CALL_INIT_WITH_EXPORTS(PDFPageDriver::Init)
542 CALL_INIT_WITH_EXPORTS(PageContentContextDriver::Init)
543 CALL_INIT_WITH_EXPORTS(FormXObjectDriver::Init)
544 CALL_INIT_WITH_EXPORTS(ObjectsContextDriver::Init);
545 CALL_INIT_WITH_EXPORTS(DocumentCopyingContextDriver::Init);
546 CALL_INIT_WITH_EXPORTS(PDFReaderDriver::Init)
547 CALL_INIT_WITH_EXPORTS(PDFTextStringDriver::Init)
548 CALL_INIT_WITH_EXPORTS(PDFDateDriver::Init)
549 CALL_INIT_WITH_EXPORTS(InputFileDriver::Init)
550 CALL_INIT_WITH_EXPORTS(OutputFileDriver::Init)
551 CALL_INIT_WITH_EXPORTS(PDFStreamDriver::Init)
552 CALL_INIT_WITH_EXPORTS(ByteReaderDriver::Init)
553 CALL_INIT_WITH_EXPORTS(ByteWriterDriver::Init)
554 CALL_INIT_WITH_EXPORTS(ByteWriterWithPositionDriver::Init)
555 CALL_INIT_WITH_EXPORTS(PDFPageModifierDriver::Init)
556 CALL_INIT_WITH_EXPORTS(ByteReaderWithPositionDriver::Init)
557 CALL_INIT_WITH_EXPORTS(DictionaryContextDriver::Init)
558 CALL_INIT_WITH_EXPORTS(DocumentContextDriver::Init)
559 CALL_INIT_WITH_EXPORTS(InfoDictionaryDriver::Init)
560 CALL_INIT_WITH_EXPORTS(ImageXObjectDriver::Init)
561 CALL_INIT_WITH_EXPORTS(PDFArrayDriver::Init)
562 CALL_INIT_WITH_EXPORTS(PDFDictionaryDriver::Init)
563 CALL_INIT_WITH_EXPORTS(PDFStreamInputDriver::Init)
564 CALL_INIT_WITH_EXPORTS(PDFIndirectObjectReferenceDriver::Init)
565 CALL_INIT_WITH_EXPORTS(PDFBooleanDriver::Init)
566 CALL_INIT_WITH_EXPORTS(PDFLiteralStringDriver::Init)
567 CALL_INIT_WITH_EXPORTS(PDFHexStringDriver::Init)
568 CALL_INIT_WITH_EXPORTS(PDFNullDriver::Init)
569 CALL_INIT_WITH_EXPORTS(PDFNameDriver::Init)
570 CALL_INIT_WITH_EXPORTS(PDFIntegerDriver::Init)
571 CALL_INIT_WITH_EXPORTS(PDFRealDriver::Init)
572 CALL_INIT_WITH_EXPORTS(PDFSymbolDriver::Init)
573 CALL_INIT_WITH_EXPORTS(PDFPageInputDriver::Init)
574 CALL_INIT_WITH_EXPORTS(PDFObjectParserDriver::Init)
575 CALL_INIT_WITH_EXPORTS(XObjectContentContextDriver::Init)
576 CALL_INIT_WITH_EXPORTS(ResourcesDictionaryDriver::Init)
577 CALL_INIT_WITH_EXPORTS(UsedFontDriver::Init)
578
579
580 // define methods
581 EXPORTS_SET(exports,NEW_SYMBOL("createWriter"), NEW_FUNCTION_TEMPLATE_EXTERNAL(CreateWriter)->GetFunction(GET_CURRENT_CONTEXT).ToLocalChecked())
582 EXPORTS_SET(exports,NEW_SYMBOL("createWriterToContinue"), NEW_FUNCTION_TEMPLATE_EXTERNAL(CreateWriterToContinue)->GetFunction(GET_CURRENT_CONTEXT).ToLocalChecked())
583 EXPORTS_SET(exports,NEW_SYMBOL("createWriterToModify"), NEW_FUNCTION_TEMPLATE_EXTERNAL(CreateWriterToModify)->GetFunction(GET_CURRENT_CONTEXT).ToLocalChecked())
584 EXPORTS_SET(exports,NEW_SYMBOL("createReader"), NEW_FUNCTION_TEMPLATE_EXTERNAL(CreateReader)->GetFunction(GET_CURRENT_CONTEXT).ToLocalChecked())
585 EXPORTS_SET(exports,NEW_SYMBOL("recrypt"), NEW_FUNCTION_TEMPLATE(Recrypt)->GetFunction(GET_CURRENT_CONTEXT).ToLocalChecked())
586
587 // define pdf versions enum
588 EXPORTS_SET(exports,NEW_SYMBOL("ePDFVersion10"),NEW_NUMBER(ePDFVersion10))
589 EXPORTS_SET(exports,NEW_SYMBOL("ePDFVersion11"),NEW_NUMBER(ePDFVersion11))
590 EXPORTS_SET(exports,NEW_SYMBOL("ePDFVersion12"),NEW_NUMBER(ePDFVersion12))
591 EXPORTS_SET(exports,NEW_SYMBOL("ePDFVersion13"),NEW_NUMBER(ePDFVersion13))
592 EXPORTS_SET(exports,NEW_SYMBOL("ePDFVersion14"),NEW_NUMBER(ePDFVersion14))
593 EXPORTS_SET(exports,NEW_SYMBOL("ePDFVersion15"),NEW_NUMBER(ePDFVersion15))
594 EXPORTS_SET(exports,NEW_SYMBOL("ePDFVersion16"),NEW_NUMBER(ePDFVersion16))
595 EXPORTS_SET(exports,NEW_SYMBOL("ePDFVersion17"),NEW_NUMBER(ePDFVersion17))
596 EXPORTS_SET(exports,NEW_SYMBOL("ePDFVersionUndefined"),NEW_NUMBER(ePDFVersionUndefined))
597
598 // procsets for resource inclusion
599 EXPORTS_SET(exports,NEW_SYMBOL("KProcsetImageB"),NEW_STRING(KProcsetImageB.c_str()))
600 EXPORTS_SET(exports,NEW_SYMBOL("KProcsetImageC"),NEW_STRING(KProcsetImageC.c_str()))
601 EXPORTS_SET(exports,NEW_SYMBOL("KProcsetImageI"),NEW_STRING(KProcsetImageI.c_str()))
602 EXPORTS_SET(exports,NEW_SYMBOL("kProcsetPDF"),NEW_STRING(KProcsetPDF.c_str()))
603 EXPORTS_SET(exports,NEW_SYMBOL("kProcsetText"),NEW_STRING(KProcsetText.c_str()))
604
605 // page range values
606 EXPORTS_SET(exports,NEW_SYMBOL("eRangeTypeAll"),NEW_NUMBER(PDFPageRange::eRangeTypeAll))
607 EXPORTS_SET(exports,NEW_SYMBOL("eRangeTypeSpecific"),NEW_NUMBER(PDFPageRange::eRangeTypeSpecific))
608
609 // EPDFPageBox enumerator, for embedding pages into forms
610 EXPORTS_SET(exports,NEW_SYMBOL("ePDFPageBoxMediaBox"),NEW_NUMBER(ePDFPageBoxMediaBox))
611 EXPORTS_SET(exports,NEW_SYMBOL("ePDFPageBoxCropBox"),NEW_NUMBER(ePDFPageBoxCropBox))
612 EXPORTS_SET(exports,NEW_SYMBOL("ePDFPageBoxBleedBox"),NEW_NUMBER(ePDFPageBoxBleedBox))
613 EXPORTS_SET(exports,NEW_SYMBOL("ePDFPageBoxTrimBox"),NEW_NUMBER(ePDFPageBoxTrimBox))
614 EXPORTS_SET(exports,NEW_SYMBOL("ePDFPageBoxArtBox"),NEW_NUMBER(ePDFPageBoxArtBox))
615
616 // parsed object types
617 EXPORTS_SET(exports,NEW_SYMBOL("ePDFObjectBoolean"),NEW_NUMBER(PDFObject::ePDFObjectBoolean))
618 EXPORTS_SET(exports,NEW_SYMBOL("ePDFObjectLiteralString"),NEW_NUMBER(PDFObject::ePDFObjectLiteralString))
619 EXPORTS_SET(exports,NEW_SYMBOL("ePDFObjectHexString"),NEW_NUMBER(PDFObject::ePDFObjectHexString))
620 EXPORTS_SET(exports,NEW_SYMBOL("ePDFObjectNull"),NEW_NUMBER(PDFObject::ePDFObjectNull))
621 EXPORTS_SET(exports,NEW_SYMBOL("ePDFObjectName"),NEW_NUMBER(PDFObject::ePDFObjectName))
622 EXPORTS_SET(exports,NEW_SYMBOL("ePDFObjectInteger"),NEW_NUMBER(PDFObject::ePDFObjectInteger))
623 EXPORTS_SET(exports,NEW_SYMBOL("ePDFObjectReal"),NEW_NUMBER(PDFObject::ePDFObjectReal))
624 EXPORTS_SET(exports,NEW_SYMBOL("ePDFObjectArray"),NEW_NUMBER(PDFObject::ePDFObjectArray))
625 EXPORTS_SET(exports,NEW_SYMBOL("ePDFObjectDictionary"),NEW_NUMBER(PDFObject::ePDFObjectDictionary))
626 EXPORTS_SET(exports,NEW_SYMBOL("ePDFObjectIndirectObjectReference"),NEW_NUMBER(PDFObject::ePDFObjectIndirectObjectReference))
627 EXPORTS_SET(exports,NEW_SYMBOL("ePDFObjectStream"),NEW_NUMBER(PDFObject::ePDFObjectStream))
628 EXPORTS_SET(exports,NEW_SYMBOL("ePDFObjectSymbol"),NEW_NUMBER(PDFObject::ePDFObjectSymbol))
629 // getter for string represenation of type enum
630 EXPORTS_SET(exports,NEW_SYMBOL("getTypeLabel"), NEW_FUNCTION_TEMPLATE(GetTypeLabel)->GetFunction(GET_CURRENT_CONTEXT).ToLocalChecked())
631
632 // ETokenSeparator
633 EXPORTS_SET(exports,NEW_SYMBOL("eTokenSeparatorSpace"),NEW_NUMBER(eTokenSeparatorSpace))
634 EXPORTS_SET(exports,NEW_SYMBOL("eTokenSeparatorEndLine"),NEW_NUMBER(eTokenSeparatorEndLine))
635 EXPORTS_SET(exports,NEW_SYMBOL("eTokenSepratorNone"),NEW_NUMBER(eTokenSepratorNone))
636
637 // EXrefEntryType
638 EXPORTS_SET(exports,NEW_SYMBOL("eXrefEntryExisting"),NEW_NUMBER(eXrefEntryExisting))
639 EXPORTS_SET(exports,NEW_SYMBOL("eXrefEntryDelete"),NEW_NUMBER(eXrefEntryDelete))
640 EXPORTS_SET(exports,NEW_SYMBOL("eXrefEntryStreamObject"),NEW_NUMBER(eXrefEntryStreamObject))
641 EXPORTS_SET(exports,NEW_SYMBOL("eXrefEntryUndefined"),NEW_NUMBER(eXrefEntryUndefined))
642
643 // EInfoTrapped
644 EXPORTS_SET(exports,NEW_SYMBOL("EInfoTrappedTrue"),NEW_NUMBER(EInfoTrappedTrue))
645 EXPORTS_SET(exports,NEW_SYMBOL("EInfoTrappedFalse"),NEW_NUMBER(EInfoTrappedFalse))
646 EXPORTS_SET(exports,NEW_SYMBOL("EInfoTrappedUnknown"),NEW_NUMBER(EInfoTrappedUnknown))
647}
648
649NODES_MODULE(hummus, HummusInit)
\No newline at end of file