-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpdf-scrapo.js
135 lines (111 loc) · 3.02 KB
/
pdf-scrapo.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
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
const fs = require('fs');
const path = require('path');
let pdfData = null;
let parsedText = null;
let fontMap = {};
function readPDFFile(filePath) {
try {
pdfData = fs.readFileSync(filePath, 'utf8');
return pdfData;
} catch (err) {
console.error(`Could not read the PDF file: ${err.message}`);
return null;
}
}
function parsePDF() {
if (!pdfData) {
console.error('No PDF data available.');
return null;
}
const lines = pdfData.split('\n');
const text = [];
fontMap = {};
let inStream = false;
let currentFont = null;
lines.forEach(line => {
line = line.trim();
if (line === "stream") {
inStream = true;
} else if (line === "endstream") {
inStream = false;
} else if (line.startsWith('/BaseFont')) {
const match = /\/BaseFont\s\/(\w+)(?:-Bold|-Oblique)?/.exec(line);
if (match) {
currentFont = match[1];
fontMap[currentFont] = match[0];
}
} else if (inStream) {
const match = /\((.*)\) Tj/.exec(line);
if (match) {
text.push({ text: match[1], font: currentFont });
}
}
});
parsedText = text;
return parsedText;
}
function processParsedText(style) {
if (!parsedText) {
console.error('No parsed text available.');
return null;
}
const styleMap = {
Italic: '-Oblique',
Bold: '-Bold'
};
if (!styleMap[style]) {
console.error('Invalid style provided.');
return null;
}
parsedText = parsedText.map(item => {
if (item.font) {
item.font = `${item.font}${styleMap[style]}`;
}
return item;
});
return parsedText;
}
function replace(parsedText, newText) {
if (!parsedText) {
console.error('No parsed text provided.');
return null;
}
if (!newText || newText.length !== parsedText.length) {
console.error('Invalid new text provided.');
return null;
}
const newContent = parsedText.map((item, index) => ({
text: newText[index],
font: item.font
}));
let updatedPDF = pdfData;
parsedText.forEach((item, index) => {
const regex = new RegExp(`\\(${item.text}\\) Tj`, 'g');
updatedPDF = updatedPDF.replace(regex, `(${newContent[index].text}) Tj`);
});
pdfData = updatedPDF;
return newContent;
}
function saveToFile(filename) {
if (!pdfData || !parsedText) {
console.error('No data available.');
return;
}
const combinedContent = `\n\n${pdfData}\n\n`;
fs.writeFileSync(filename, combinedContent, 'utf8');
}
function processPDF(inputFilePath, outputFilePath) {
readPDFFile(inputFilePath);
parsePDF();
saveToFile(outputFilePath);
}
module.exports = {
readPDFFile,
parsePDF,
processParsedText,
replace,
saveToFile,
processPDF,
getPdfData: () => pdfData,
getParsedText: () => parsedText
};