@docscode/adapter-pdf 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/package.json +17 -0
  2. package/src/index.ts +48 -0
package/package.json ADDED
@@ -0,0 +1,17 @@
1
+ {
2
+ "name": "@docscode/adapter-pdf",
3
+ "version": "1.0.0",
4
+ "description": "PDF Format Adapter for Kairo",
5
+ "main": "./dist/index.cjs",
6
+ "module": "./dist/index.js",
7
+ "types": "./dist/index.d.ts",
8
+ "type": "module",
9
+ "scripts": {
10
+ "build": "tsup src/index.ts --format cjs,esm --dts --clean"
11
+ },
12
+ "dependencies": {
13
+ "@docscode/core": "*",
14
+ "pdf-lib": "^1.17.1",
15
+ "yjs": "^13.6.30"
16
+ }
17
+ }
package/src/index.ts ADDED
@@ -0,0 +1,48 @@
1
+ import * as Y from 'yjs';
2
+ import { FormatAdapter, CanonicalDoc, DoclingClient } from '@docscode/core';
3
+ import { PDFDocument, rgb } from 'pdf-lib';
4
+
5
+ export class PdfAdapter implements FormatAdapter {
6
+ readonly format = 'pdf';
7
+ private docling = new DoclingClient();
8
+
9
+ async read(source: Buffer | string): Promise<Y.Doc> {
10
+ const yDoc = new Y.Doc();
11
+ const canonical = new CanonicalDoc(yDoc);
12
+
13
+ const input = Buffer.isBuffer(source)
14
+ ? `base64:${source.toString('base64')}`
15
+ : source;
16
+
17
+ const result = await this.docling.convert(input);
18
+
19
+ for (const block of result.content) {
20
+ if (block.type === 'p') {
21
+ canonical.addParagraph(block.text);
22
+ }
23
+ }
24
+
25
+ return yDoc;
26
+ }
27
+
28
+ async write(doc: Y.Doc, original?: Buffer): Promise<Buffer> {
29
+ if (!original) {
30
+ throw new Error('PDF write adapter requires the original buffer to apply annotations.');
31
+ }
32
+
33
+ const pdfDoc = await PDFDocument.load(original);
34
+ const pages = pdfDoc.getPages();
35
+ const firstPage = pages[0];
36
+
37
+ // Placeholder for AI Annotation Layer
38
+ // In a real implementation, we would map CRDT changes to specific coordinates
39
+ firstPage.drawText('-- Kairo AI Annotation Overlay --', {
40
+ x: 50,
41
+ y: 50,
42
+ size: 10,
43
+ color: rgb(0.95, 0.1, 0.1),
44
+ });
45
+
46
+ return Buffer.from(await pdfDoc.save());
47
+ }
48
+ }