Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,5 @@ logs
results

npm-debug.log
dist
node_modules/
18 changes: 18 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Launch Program",
"program": "${workspaceFolder}/index.ts",
"preLaunchTask": "tsc: build - tsconfig.json",
"outFiles": [
"${workspaceFolder}/dist/**/*.js"
]
}
]
}
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,11 @@ Response should include something like this:
##More Info

[Restful Web Service for PDF2JSON](http://www.codeproject.com/Articles/573297/Restful-Web-Service-for-PDF2JSON)


## typescript

```
npx tsc
node dist/index.js
```
4 changes: 2 additions & 2 deletions index.js → index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@
//curl -isv -H "Content-Type: application/json" -X POST -d '{"folderName":"data", "pdfId":"xfa_1040"}' http://0.0.0.0:8001/p2jsvc

'use strict';
var service = require("./lib/service");
service.start();
import {PDFFORMService} from './lib/service';
(new PDFFORMService()).start();
121 changes: 0 additions & 121 deletions lib/service.js

This file was deleted.

119 changes: 119 additions & 0 deletions lib/service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
'use strict';
import log from "npmlog";
import { plugins, pre, createServer, Response } from "restify";
import { SvcContext } from './svccontext';
import { SvcResponse } from './svcresponse';
import PDFParser, { PDFDataReady, PDFFormImage, PDFParserError } from 'pdf2json';


export class PDFFORMService {
// private static
private static _nextId = 1;
private static _name = 'PDFFORMServer';
private static _pdfPathBase = "";

get_id: () => number;
get_name: () => string;
get_version: () => string;

// constructor
constructor() {
// private, only accessible within this constructor
let _id = PDFFORMService._nextId++;
let _name = PDFFORMService._name;
const _version = "0.0.1";

// public (every instance will have their own copy of these methods, needs to be lightweight)
this.get_id = function() { return _id; };
this.get_name = function() { return _name + _id; };
this.get_version = function() {return _version; };
};

// public static
public static get_nextId() {
return this._name + this._nextId;
};

//private
private _onPFBinDataReady(context: SvcContext , evtData: PDFFormImage) {
log.info(this.get_name(), " completed response.");
var resData = new SvcResponse(200, "OK", "data", evtData);
context.completeResponse(resData);
resData.destroy();
evtData = null;
return context.next();
};

private _onPFBinDataError(context: SvcContext, error: PDFParserError) {
log.info(this.get_name() + " 500 Error: ", JSON.stringify(error));
var resData = new SvcResponse(500, JSON.stringify(error), undefined, undefined)
context.completeResponse(resData);
resData.destroy();
resData = null;
return context.next();
};

_customizeHeaders(res: Response) {
// Resitify currently has a bug which doesn't allow you to set default headers
// This headers comply with CORS and allow us to server our response to any origin
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "X-Requested-With");
res.header("Cache-Control", "no-cache, must-revalidate");
};

// public (every instance will share the same method, but has no access to private fields defined in constructor)
start() {
//private function within this public method
const version = this.get_version();
const _gfilter = (svcContext: SvcContext) => {
var req = svcContext.request;
var folderName = req.params.folderName;
var pdfId = req.params.pdfId;
log.info(this.get_name(), " received request:" + req.method + ":" + folderName + "/" + pdfId);

var pdfParser = new PDFParser();

this._customizeHeaders(svcContext.response);

pdfParser.on("pdfParser_dataReady", (data: PDFFormImage) => this._onPFBinDataReady(svcContext, data));
pdfParser.on("pdfParser_dataError", (error: PDFParserError) => this._onPFBinDataError(svcContext, error));

pdfParser.loadPDF(PDFFORMService._pdfPathBase + folderName + "/" + pdfId + ".pdf", 0);
};

const server = createServer({
name: this.get_name(),
version: this.get_version()
});

server.use(plugins.acceptParser(server.acceptable));
server.use(plugins.authorizationParser());
server.use(plugins.dateParser());
server.use(plugins.queryParser());
server.use(plugins.bodyParser());
server.use(plugins.jsonp());
server.use(plugins.gzipResponse());
server.pre(pre.userAgentConnection());

server.get('/p2jsvc/:folderName/:pdfId', function(req, res, next) {
_gfilter(new SvcContext(req, res, next));
});

server.post('/p2jsvc', function(req, res, next) {
_gfilter(new SvcContext(req, res, next));
});

server.get('/p2jsvc/status', function(req, res, next) {
var jsObj = new SvcResponse(200, "OK", server.name, version);
res.send(200, jsObj);
return next();
});

server.listen(7799, function() {
log.info(server.name, ' listening at ' + server.url);
});
};
};



25 changes: 0 additions & 25 deletions lib/svccontext.js

This file was deleted.

26 changes: 26 additions & 0 deletions lib/svccontext.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
'use strict';
import {Request, Response, Next} from 'restify';

export class SvcContext {
request: Request;
response: Response;
next: Next;
// constructor
constructor(req: Request, res: Response, next: Next) {
// public, this instance copies
this.request = req;
this.response = res;
this.next = next;
};

public completeResponse(jsObj) {
this.response.send(200, jsObj);
this.next();
};

public destroy() {
this.request = null;
this.response = null;
this.next = null;
};
}
32 changes: 0 additions & 32 deletions lib/svcresponse.js

This file was deleted.

Loading