Skip to content

Commit 1798c91

Browse files
committed
firstish
1 parent 47c10c6 commit 1798c91

16 files changed

+222
-0
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules/

example/annotations.js

+24
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

example/helloworld.pdf

678 Bytes
Binary file not shown.

example/index.html

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8"/>
5+
<title>PDFJSAnnotate</title>
6+
</head>
7+
<body>
8+
<canvas id="canvas" style="border: 1px solid black;"></canvas>
9+
<script src="bundle.js"></script>
10+
</body>
11+
</html>

example/index.js

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import __pdfjs from 'pdfjs-dist/build/pdf.js';
2+
import PDFJSAnnotate from '../';
3+
import annotations from './annotations';
4+
5+
PDFJS.workerSrc = '../node_modules/pdfjs-dist/build/pdf.worker.js';
6+
7+
PDFJSAnnotate.StoreAdapter.getAnnotations = (documentId, pageNumber) => {
8+
return new Promise((resolve, reject) => {
9+
resolve(annotations);
10+
});
11+
};
12+
13+
PDFJS.getDocument('helloworld.pdf').then((pdf) => {
14+
Promise.all([
15+
pdf.getPage(1),
16+
PDFJSAnnotate.getAnnotations(1)
17+
])
18+
.then(([page, annotations]) => {
19+
let scale = 3.5;
20+
let viewport = page.getViewport(scale);
21+
let canvas = document.getElementById('canvas');
22+
let canvasContext = canvas.getContext('2d');
23+
24+
canvas.height = viewport.height;
25+
canvas.width = viewport.width;
26+
27+
page.render({canvasContext, viewport});
28+
PDFJSAnnotate.render(viewport, annotations);
29+
});
30+
});

index.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import PDFJSAnnotate from './src/PDFJSAnnotate';
2+
3+
export default PDFJSAnnotate;

package.json

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{
2+
"name": "pdf-annotate.js",
3+
"version": "0.0.0",
4+
"description": "Annotation layer for pdf.js",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1",
8+
"start": "./node_modules/.bin/webpack-dev-server --inline --config ./webpack.example.js --content-base ./"
9+
},
10+
"repository": {
11+
"type": "git",
12+
"url": "git+https://github.com/mzabriskie/pdf-annotate.js.git"
13+
},
14+
"keywords": [
15+
"pdf",
16+
"annotation"
17+
],
18+
"author": "Matt Zabriskie",
19+
"license": "MIT",
20+
"bugs": {
21+
"url": "https://github.com/mzabriskie/pdf-annotate.js/issues"
22+
},
23+
"homepage": "https://github.com/mzabriskie/pdf-annotate.js#readme",
24+
"devDependencies": {
25+
"babel-core": "5.8.22",
26+
"babel-loader": "5.3.2",
27+
"pdfjs-dist": "1.1.399",
28+
"webpack": "1.11.0",
29+
"webpack-dev-server": "1.10.1"
30+
}
31+
}

src/AnnotateView.js

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import renderSVG from './render/renderSVG';
2+
import renderDrawing from './render/renderDrawing';
3+
4+
export default class AnnotateView {
5+
constructor(viewport, annotations) {
6+
this.viewport = viewport;
7+
this.annotations = annotations;
8+
}
9+
10+
render() {
11+
let svg = renderSVG(this.viewport);
12+
13+
this.annotations.forEach((a) => {
14+
let el;
15+
switch (a.type) {
16+
case 'drawing':
17+
el = renderDrawing(a);
18+
break;
19+
}
20+
21+
svg.appendChild(el);
22+
});
23+
24+
document.body.appendChild(svg);
25+
}
26+
}

src/PDFJSAnnotate.js

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import StoreAdapter from './StoreAdapter';
2+
import AnnotateView from './AnnotateView';
3+
4+
// Public API
5+
let PDFJSAnnotate = {
6+
StoreAdapter,
7+
8+
getAnnotations(documentId, pageNumber) {
9+
return this.StoreAdapter.getAnnotations(documentId, pageNumber);
10+
},
11+
12+
render(viewport, annotations) {
13+
let view = new AnnotateView(viewport, annotations);
14+
view.render();
15+
}
16+
};
17+
18+
export default PDFJSAnnotate;

src/StoreAdapter.js

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import abstractFunction from './utils/abstractFunction';
2+
3+
// Adapter should never be invoked publicly
4+
let StoreAdapter = {
5+
getAnnotations: abstractFunction('getAnnotations'),
6+
addAnnotation: abstractFunction('addAnnotation'),
7+
editAnnotation: abstractFunction('editAnnotation'),
8+
deleteAnnotation: abstractFunction('deleteAnnotation')
9+
};
10+
11+
export default StoreAdapter;

src/render/renderDrawing.js

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
export default function renderDrawing(a) {
2+
let d = [];
3+
let path = document.createElementNS('http://www.w3.org/2000/svg', 'path');
4+
5+
for (let i=0, l=a.lines.length; i<l; i++) {
6+
var p1 = a.lines[i];
7+
var p2 = a.lines[i+1];
8+
if (p2) {
9+
d.push('M' + p1[0] + ' ' + p1[1] + ' ' + p2[0] + ' ' + p2[1]);
10+
}
11+
}
12+
13+
path.setAttribute('d', d.join(',') + 'Z');
14+
path.setAttribute('stroke', '#' + a.color);
15+
path.setAttribute('fill', 'none');
16+
17+
return path;
18+
}

src/render/renderSVG.js

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
export default function renderSVG(viewport) {
2+
let svg = document.createElementNS('http://www.w3.org/2000/svg', 'svg');
3+
4+
svg.style.display = 'block';
5+
svg.style.position = 'absolute';
6+
svg.style.top = viewport.offsetY + 'px';
7+
svg.style.left = viewport.offsetX + 'px';
8+
svg.style.width = viewport.width + 'px';
9+
svg.style.height = viewport.height + 'px';
10+
// svg.style.background = 'rgba(0, 0, 0, .2)';
11+
12+
return svg;
13+
}

src/utils/abstractFunction.js

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export default function abstractFunction(name) {
2+
return function () {
3+
throw new Error(name + ' is not implemented');
4+
};
5+
}

src/utils/uuid.js

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export default function uuid() {
2+
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
3+
var r = Math.random()*16|0, v = c == 'x' ? r : (r&0x3|0x8);
4+
return v.toString(16);
5+
});
6+
}

webpack.config.js

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
module.exports = {
2+
entry: './index.js',
3+
output: {
4+
filename: 'dist/pdf-annotate.js',
5+
library: 'PDFAnnotate',
6+
libraryTarget: 'umd'
7+
},
8+
module: {
9+
loaders: [
10+
{ test: /\.js$/, exclude: /node_modules/, loader: 'babel-loader' }
11+
]
12+
}
13+
};

webpack.example.js

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
module.exports = {
2+
entry: './example/index.js',
3+
output: {
4+
filename: './example/bundle.js',
5+
},
6+
module: {
7+
loaders: [
8+
{ test: /\.js$/, exclude: /node_modules/, loader: 'babel-loader' }
9+
]
10+
}
11+
};
12+

0 commit comments

Comments
 (0)