Replies: 1 comment
-
Closing since this already seems tracked in #18411. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
`import express from 'express';
import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';
const pdfjsLib = await import('pdfjs-dist/legacy/build/pdf.mjs');
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const app = express();
const data = new Uint8Array(fs.readFileSync(path.join(__dirname, './TentPDF.pdf')));
let globalWidth = 0;
let globalHeight = 0;
const OPS = {
moveTo: 13,
lineTo: 14,
curveTo: 15,
curveTo2: 16,
curveTo3: 17,
closePath: 18,
rectangle: 19,
constructPath: 91,
beginMarkedContentProps: 70
};
function mapConstructPath(args) {
const [ops, coords] = args;
const result = [];
let index = 0;
for (let i = 0; i < ops.length; i++) {
const op = ops[i];
switch (op) {
case OPS.moveTo:
if (coords.length >= index + 2) {
result.push({ op: "m", to: [coords[index], coords[index + 1]] });
index += 2;
}
break;
case OPS.lineTo:
if (coords.length >= index + 2) {
result.push({ op: "l", to: [coords[index], coords[index + 1]] });
index += 2;
}
break;
case OPS.curveTo:
if (coords.length >= index + 6) {
result.push({
op: "c",
to: [coords[index + 4], coords[index + 5]],
c1: [coords[index], coords[index + 1]],
c2: [coords[index + 2], coords[index + 3]]
});
index += 6;
}
break;
case OPS.curveTo2:
if (coords.length >= index + 4) {
result.push({
op: "v",
to: [coords[index + 2], coords[index + 3]],
c1: [coords[index], coords[index + 1]]
});
index += 4;
}
break;
case OPS.curveTo3:
if (coords.length >= index + 4) {
result.push({
op: "y",
to: [coords[index + 2], coords[index + 3]],
c2: [coords[index], coords[index + 1]]
});
index += 4;
}
break;
case OPS.closePath:
result.push({ op: "h" });
break;
case OPS.rectangle:
if (coords.length >= index + 4) {
const x = coords[index];
const y = coords[index + 1];
const width = coords[index + 2];
const height = coords[index + 3];
}
return result;
}
pdfjsLib.getDocument({ data: data }).promise.then(async function(pdfDocument) {
const optionalContentConfig = await pdfDocument.getOptionalContentConfig();
const layerNames = [];
const layerOperations = {};
const ocgIdToNameMap = {};
const groups = optionalContentConfig.getGroups();
Object.keys(groups).forEach(groupId => {
const group = groups[groupId];
layerNames.push(group.name);
layerOperations[group.name] = { actions: [] };
ocgIdToNameMap[groupId] = group.name;
});
console.log('Layer Names:', layerNames);
console.log('OCG ID to Name Map:', ocgIdToNameMap);
const numPages = pdfDocument.numPages;
let currentLayerId = -1;
for (let pageNum = 1; pageNum <= numPages; pageNum++) {
const page = await pdfDocument.getPage(pageNum);
if (pageNum === 1) {
const viewport = page.getViewport({ scale: 1.0 });
globalWidth = viewport.width;
globalHeight = viewport.height;
}
const operatorList = await page.getOperatorList();
console.log('Operator List for Page', pageNum, operatorList);
}
const result = {
w: globalWidth,
h: globalHeight,
userMask: {
actions: []
},
...layerOperations
};
const outputPath = path.join(__dirname, 'layerOperations.json');
fs.writeFileSync(outputPath, JSON.stringify(result, null, 2));
console.log(
Graphics data saved to ${outputPath}
);}).catch(function(error) {
console.error('Error: ' + error);
});
app.listen(3000, (err) => {

if (!err) console.log("Server started success!port:3000");
});
**This is my code** [TentPDF.pdf](https://github.com/user-attachments/files/16138762/TentPDF.pdf) **This is the PDF where I need to obtain coordinate data**
{"w": 10351,
"h": 8348.25,
"userMask": {
"actions": []
},
"rtpmask": {
"actions": [
{
"op": "m",
"to": [
0,
8348.247
]
},
{
"op": "l",
"to": [
10351,
8348.247
]
},
{
"op": "l",
"to": [
10351,
0
]
},
{
"op": "l",
"to": [
0,
0
]
},
{
"op": "h"
},
{
"op": "m",
"to": [
0,
0
]
},
{
"op": "l",
"to": [
4516,
0
]
},
{
"op": "l",
"to": [
4516,
1204
]
},
{
"op": "c",
"to": [
2152.5,
3720
],
"c1": [
3360,
2304
],
"c2": [
2216.5,
3648
]
},
{
"op": "y",
"to": [
0,
6649.581
],
"c2": [
792.5,
5352
]
},
{
"op": "v",
"to": [
-2152.5,
3720
],
"c1": [
-792.5,
5352
]
},
{
"op": "c",
"to": [
-4516,
1204
],
"c1": [
-2216.5,
3648
],
"c2": [
-3360,
2304
]
},
{
"op": "l",
"to": [
-4516,
0
]
},
{
"op": "l",
"to": [
0,
0
]
},
{
"op": "h"
},
{
"op": "m",
"to": [
0,
0
]
},
{
"op": "v",
"to": [
-2152.5,
-2929.58
],
"c1": [
-792.5,
-1297.58
]
},
{
"op": "c",
"to": [
-4516,
-5445.58
],
"c1": [
-2216.5,
-3001.58
],
"c2": [
-3360,
-4345.58
]
},
{
"op": "l",
"to": [
-4516,
-6649.58
]
},
{
"op": "l",
"to": [
0,
-6649.58
]
},
{
"op": "l",
"to": [
4516,
-6649.58
]
},
{
"op": "l",
"to": [
4516,
-5445.58
]
},
{
"op": "c",
"to": [
2152.5,
-2929.58
],
"c1": [
3360,
-4345.58
],
"c2": [
2216.5,
-3001.58
]
},
{
"op": "y",
"to": [
0,
0
],
"c2": [
792.5,
-1297.58
]
},
{
"op": "m",
"to": [
0,
-13.35
]
},
{
"op": "c",
"to": [
139.207,
-232.059
],
"c1": [
15.888,
-39.052
],
"c2": [
63.217,
-115.029
]
},
{
"op": "c",
"to": [
560.507,
-856.791
],
"c1": [
226.412,
-366.359
],
"c2": [
370.292,
-583.762
]
},
{
"op": "c",
"to": [
1236.839,
-1787.555
],
"c1": [
774.294,
-1163.655
],
"c2": [
1001.846,
-1476.81
]
},
{
"op": "c",
"to": [
2147.123,
-2934.061
],
"c1": [
1530.578,
-2175.984
],
"c2": [
1836.842,
-2561.724
]
},
{
"op": "l",
"to": [
2147.195,
-2934.146
]
},
{
"op": "l",
"to": [
2147.268,
-2934.229
]
},
{
"op": "c",
"to": [
2185.727,
-2978.532
],
"c1": [
2152.628,
-2940.261
],
"c2": [
2165.795,
-2955.485
]
},
{
"op": "c",
"to": [
4509,
-5448.581
],
"c1": [
3001.652,
-3921.985
],
"c2": [
3826.597,
-4799.045
]
},
{
"op": "l",
"to": [
4509,
-6642.58
]
},
{
"op": "l",
"to": [
0,
-6642.58
]
},
{
"op": "l",
"to": [
-4509,
-6642.58
]
},
{
"op": "l",
"to": [
-4509,
-5448.581
]
},
{
"op": "c",
"to": [
-2185.727,
-2978.532
],
"c1": [
-3826.596,
-4799.045
],
"c2": [
-3001.652,
-3921.985
]
},
{
"op": "c",
"to": [
-2147.268,
-2934.229
],
"c1": [
-2165.796,
-2955.485
],
"c2": [
-2152.628,
-2940.261
]
},
{
"op": "l",
"to": [
-2147.195,
-2934.146
]
},
{
"op": "l",
"to": [
-2147.122,
-2934.061
]
},
{
"op": "c",
"to": [
-1236.839,
-1787.555
],
"c1": [
-1836.843,
-2561.724
],
"c2": [
-1530.578,
-2175.984
]
},
{
"op": "c",
"to": [
-560.506,
-856.791
],
"c1": [
-1001.845,
-1476.81
],
"c2": [
-774.294,
-1163.655
]
},
{
"op": "c",
"to": [
-139.207,
-232.059
],
"c1": [
-370.291,
-583.762
],
"c2": [
-226.411,
-366.359
]
},
{
"op": "c",
"to": [
0,
-13.35
],
"c1": [
-63.217,
-115.029
],
"c2": [
-15.888,
-39.052
]
}
]
},
"mask": {
"actions": [
{
"op": "m",
"to": [
0,
8348.247
]
},
{
"op": "l",
"to": [
10351,
8348.247
]
},
{
"op": "l",
"to": [
10351,
0
]
},
{
"op": "l",
"to": [
0,
0
]
},
{
"op": "h"
},
{
"op": "m",
"to": [
0,
0
]
},
{
"op": "c",
"to": [
-179.983,
255.207
],
"c1": [
-65.391,
91.632
],
"c2": [
-125.466,
176.953
]
},
{
"op": "c",
"to": [
-628.082,
921.071
],
"c1": [
-382.099,
545.315
],
"c2": [
-532.479,
772.978
]
},
{
"op": "c",
"to": [
-1076.179,
255.207
],
"c1": [
-723.684,
772.978
],
"c2": [
-874.064,
545.315
]
},
{
"op": "c",
"to": [
-1253.344,
3.95
],
"c1": [
-1129.913,
178.079
],
"c2": [
-1189.045,
94.085
]
},
{
"op": "l",
"to": [
0,
0
]
},
{
"op": "h"
},
{
"op": "m",
"to": [
0,
0
]
},
{
"op": "l",
"to": [
-8328.275,
0
]
},
{
"op": "l",
"to": [
-8328.274,
-0.001
]
},
{
"op": "c",
"to": [
-6246.241,
2237.432
],
"c1": [
-7696.294,
620.653
],
"c2": [
-6967.552,
1403.381
]
},
{
"op": "c",
"to": [
-6209.011,
2280.331
],
"c1": [
-6227.379,
2259.24
],
"c2": [
-6213.752,
2274.997
]
},
{
"op": "l",
"to": [
-6207.484,
2282.049
]
},
{
"op": "l",
"to": [
-6206.015,
2283.814
]
},
{
"op": "c",
"to": [
-4961.869,
3880
],
"c1": [
-5706.781,
2882.893
],
"c2": [
-5285.271,
3436.079
]
},
{
"op": "l",
"to": [
-3366.407,
3880
]
},
{
"op": "c",
"to": [
-2122.261,
2283.814
],
"c1": [
-3043.005,
3436.079
],
"c2": [
-2621.494,
2882.893
]
},
{
"op": "l",
"to": [
-2120.791,
2282.049
]
},
{
"op": "l",
"to": [
-2119.265,
2280.331
]
},
{
"op": "c",
"to": [
-2082.034,
2237.432
],
"c1": [
-2114.524,
2274.997
],
"c2": [
-2100.896,
2259.24
]
},
{
"op": "c",
"to": [
-0.001,
-0.001
],
"c1": [
-1360.723,
1403.381
],
"c2": [
-631.981,
620.653
]
},
{
"op": "l",
"to": [
0,
0
]
},
{
"op": "h"
},
{
"op": "m",
"to": [
803.5,
2165.333
]
},
{
"op": "l",
"to": [
9547.5,
2165.333
]
},
{
"op": "l",
"to": [
9547.5,
1341.333
]
},
{
"op": "l",
"to": [
803.5,
1341.333
]
},
{
"op": "h"
}
]
}
}`
This is the data I obtained
My question:
Beta Was this translation helpful? Give feedback.
All reactions