-
-
Notifications
You must be signed in to change notification settings - Fork 16
/
parseFromPdf.js
195 lines (161 loc) · 5.53 KB
/
parseFromPdf.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
136
137
138
139
140
141
142
143
144
145
146
147
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
const shell = require('shelljs');
const jsdom = require('jsdom');
const fs = require('fs');
const puppeteer = require('puppeteer');
const path = require('path');
const { compressSVG, scale } = require('./utils');
const X_DATE_MIN = new Date(Date.UTC(2020, 1, 29, 0, 0, 1));
const X_DATE_MAX = new Date(Date.UTC(2020, 3, 11, 23, 59, 59));
const Y_PERCENT_MAX = 80;
async function pdfToSVG(pdfFile, pageNumber, exportPath) {
console.log(
`inkscape --pdf-poppler --export-type=svg --export-plain-svg --export-area-page --export-filename=${exportPath} --pdf-page ${pageNumber} --vacuum-defs ${pdfFile} `
);
shell.exec(
`inkscape --pdf-poppler --export-type=svg --export-plain-svg --export-area-page --export-filename=${exportPath} --pdf-page ${pageNumber} --vacuum-defs ${pdfFile}`
);
const svg = fs.readFileSync(exportPath, 'utf8');
fs.writeFileSync(exportPath, svg, 'utf8');
return svg;
}
async function getPageCanvas(content) {
const virtualConsole = new jsdom.VirtualConsole();
virtualConsole.sendTo(console);
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.setContent(content);
await page.addScriptTag({ path: './lib/flatten.js' });
page.on('console', (msg) => {
for (let i = 0; i < msg._args.length; ++i) console.log(`${i}: ${msg._args[i]}`);
});
return { page, browser };
}
async function stripSVG(page) {
return page.evaluate(() => {
const RESOLUTION = 3000;
const svg = document.querySelector('svg');
const blacklistedSelectors = [
'symbol',
'text',
'tspan',
'path:not([d])',
'g:empty',
'metadata',
'mask',
'filter',
];
const removeElement = (element) => element.parentNode.removeChild(element);
const gridlineStroke = 'rgb(218, 220, 224)';
const seriesStroke = 'rgb(66, 133, 244)';
const seriesPaths = [];
const gridPaths = [];
const mapPoints = (path) => {
const pathLength = path.getTotalLength();
const points = [];
for (let i = 0; i <= pathLength; i = i + pathLength / RESOLUTION) {
const point = path.getPointAtLength(i);
points.push({
x: point.x,
y: point.y,
});
}
points.push({
x: path.getPointAtLength(pathLength).x,
y: path.getPointAtLength(pathLength).y,
});
return points;
};
flattenSVG(svg);
blacklistedSelectors.forEach((selector) => {
[...document.querySelectorAll(selector)].forEach((ele) => removeElement(ele));
});
document.querySelectorAll('path').forEach((path) => {
const stroke = path.style.stroke;
const isGridStroke = stroke === gridlineStroke;
const isSeriesStroke = stroke === seriesStroke;
const isLongStroke = path.getTotalLength() > 6;
if (isSeriesStroke) {
seriesPaths.push(path);
} else if (isGridStroke && isLongStroke) {
gridPaths.push(path);
} else {
removeElement(path);
}
});
document.querySelectorAll('g').forEach((g) => {
if (!g.innerHTML.trim()) {
removeElement(g);
}
});
let charts = [];
// add series
seriesPaths.forEach((path) => {
charts.push({ seriesPath: path });
});
if (gridPaths.length !== seriesPaths.length * 5) {
throw new Error(
'Each series path mush have 5 grid paths, found: gridpaths - ' +
gridPaths.length +
' and series path ' +
seriesPaths.length
);
}
// add grids
let chartIndex = 0;
for (let i = 0; i < gridPaths.length; i += 5) {
charts[chartIndex++].gridPaths = gridPaths.slice(i, i + 5);
}
// add boundaries
charts = charts.map((chart) => ({
// ...chart,
x: chart.gridPaths[0].getBBox().x,
y: chart.gridPaths[4].getBBox().y,
width: chart.gridPaths[0].getBBox().width,
height: chart.gridPaths[0].getBBox().y - chart.gridPaths[4].getBBox().y,
seriesPoints: mapPoints(chart.seriesPath),
}));
return {
svg: document.querySelector('svg').outerHTML,
charts: charts,
};
});
}
async function processPDFPage(pdfPath, pageNumber, outputPath) {
const svg = await pdfToSVG(
pdfPath,
pageNumber,
path.join(outputPath, `original-${pageNumber}.svg`)
);
const { page, browser } = await getPageCanvas(svg);
const { svg: strippedSVG, charts } = await stripSVG(page);
await browser.close();
const chartsProcessed = charts.map((chart) => {
const points = chart.seriesPoints.map((point) => ({
value:
Y_PERCENT_MAX -
scale(point.y, chart.y, chart.y + chart.height, 0, Y_PERCENT_MAX + Y_PERCENT_MAX),
timestamp: new Date(
scale(point.x, chart.x, chart.x + chart.width, X_DATE_MIN.getTime(), X_DATE_MAX.getTime())
),
}));
// downsample to be daily
let doneDates = [];
let downsampledPoints = [];
const pad = (number) => (number <= 9 ? `0${number}` : number);
const formatDate = (date) =>
`${date.getUTCFullYear()}-${pad(date.getUTCMonth() + 1)}-${pad(date.getUTCDate())}`;
for (let i = points.length - 1; i >= 0; i--) {
const date = formatDate(new Date(points[i].timestamp));
if (doneDates.includes(date)) {
continue;
}
doneDates.push(date);
downsampledPoints.push({ date, value: points[i].value });
}
return { points: downsampledPoints };
});
const svgoed = await compressSVG(strippedSVG);
fs.writeFileSync(path.join(outputPath, `processed-${pageNumber}.svg`), svgoed, 'utf8');
return chartsProcessed;
}
module.exports = { processPDFPage };