-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpdfMakeAccessible.js
68 lines (57 loc) · 2.02 KB
/
pdfMakeAccessible.js
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
/*
`pdfMakeAccessible.js`, demonstrates how to use the PDFix SDK for Node.js to convert
a PDF document into an accessible format. It opens a PDF, performs auto-tagging
to create a structure tree, adjusts the content for compliance (e.g., reading order,
alt text), and saves the accessible PDF.
*/
const Pdfix = require('pdfix-sdk');
const pdfixSdkWrapper = new Pdfix();
pdfixSdkWrapper.loadPdfixSdk().then(() => {
const pdfixSdk = pdfixSdkWrapper.getPdfixSdk();
const pdfix = pdfixSdk.GetPdfix();
const pdfDoc = pdfixSdkWrapper.openDocumentFromPath("./pdf/test.pdf");
// log document information before processing
console.log({
numOfPages: pdfDoc.GetNumPages(),
version: pdfDoc.GetVersion(),
pdfStandard: pdfDoc.GetPdfStandard()
});
const commandPath = ""; // path to custom command JSON file
const command = pdfDoc.GetCommand();
// load the make-accessible command from JSON or use the default
var cmdStm = null;
if (commandPath == "") {
cmdStm = pdfix.CreateMemStream()
if (cmdStm)
command.SaveCommandsToStream(pdfixSdk.kActionMakeAccessible, cmdStm, pdfixSdk.kDataFormatJson, pdfixSdk.kSaveFull);
}
else {
cmdStm = pdfix.CreateFileStream(commandPath, pdfixSdk.kPsReadOnly);
}
if (!cmdStm) {
throw Exception(pdfix.GetError())
}
// load command params and and run
if (!command.LoadParamsFromStream(cmdStm, pdfixSdk.kDataFormatJson)) {
throw Exception(pdfix.GetError())
}
cmdStm.Destroy();
if (!command.Run()) {
throw Exception(pdfix.GetError())
}
const docStream = pdfix.CreateMemStream();
if (!pdfDoc.SaveToStream(docStream)) {
throw Exception(pdfix.GetError())
}
// read file data to buffer
var arrayBuffer = new ArrayBuffer(docStream.GetSize());
docStream.ReadToArrayBuffer(0, arrayBuffer, arrayBuffer.byteLength);
docStream.Destroy();
// log document information after processing
console.log({
numOfPages: pdfDoc.GetNumPages(),
version: pdfDoc.GetVersion(),
pdfStandard: pdfDoc.GetPdfStandard()
});
pdfDoc.Close();
});