@journeyapps/pdf-reports
Advanced tools
Comparing version 0.4.0 to 1.0.0
"use strict"; | ||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
return new (P || (P = Promise))(function (resolve, reject) { | ||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } | ||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } | ||
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); } | ||
step((generator = generator.apply(thisArg, _arguments || [])).next()); | ||
}); | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.generatePdfDocRaptor = exports.setDocRaptorToken = void 0; | ||
const node_fetch_1 = require("node-fetch"); | ||
@@ -24,39 +17,37 @@ const pdf_1 = require("./pdf"); | ||
*/ | ||
function generatePdfDocRaptor(options) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const docraptor = options.docraptor; | ||
const apiKey = (docraptor && docraptor.user_credentials) || docRaptorToken; | ||
const body = { | ||
type: 'pdf', | ||
javascript: true, | ||
test: options.test || apiKey == null, | ||
prince_options: { | ||
no_compress: false | ||
} | ||
}; | ||
if (apiKey) { | ||
body.user_credentials = apiKey; | ||
async function generatePdfDocRaptor(options) { | ||
const docraptor = options.docraptor; | ||
const apiKey = (docraptor && docraptor.user_credentials) || docRaptorToken; | ||
const body = { | ||
type: 'pdf', | ||
javascript: true, | ||
test: options.test || apiKey == null, | ||
prince_options: { | ||
no_compress: false | ||
} | ||
if (docraptor && docraptor) { | ||
Object.assign(body, docraptor); | ||
} | ||
if (options.html) { | ||
body.document_content = options.html; | ||
} | ||
else if (options.url) { | ||
body.document_url = options.url; | ||
} | ||
const response = yield node_fetch_1.default('https://docraptor.com/docs', { | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/json' | ||
}, | ||
body: JSON.stringify(body) | ||
}); | ||
if (!response.ok) { | ||
throw new Error('Failed to generate PDF: ' + response.statusText + "\n" + (yield response.text())); | ||
} | ||
return new pdf_1.PdfResult(yield response.buffer()); | ||
}; | ||
if (apiKey) { | ||
body.user_credentials = apiKey; | ||
} | ||
if (docraptor && docraptor) { | ||
Object.assign(body, docraptor); | ||
} | ||
if (options.html) { | ||
body.document_content = options.html; | ||
} | ||
else if (options.url) { | ||
body.document_url = options.url; | ||
} | ||
const response = await (0, node_fetch_1.default)('https://docraptor.com/docs', { | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/json' | ||
}, | ||
body: JSON.stringify(body) | ||
}); | ||
if (!response.ok) { | ||
throw new Error('Failed to generate PDF: ' + response.statusText + "\n" + await response.text()); | ||
} | ||
return new pdf_1.PdfResult(await response.buffer()); | ||
} | ||
exports.generatePdfDocRaptor = generatePdfDocRaptor; |
"use strict"; | ||
function __export(m) { | ||
for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p]; | ||
} | ||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); | ||
}) : (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
o[k2] = m[k]; | ||
})); | ||
var __exportStar = (this && this.__exportStar) || function(m, exports) { | ||
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p); | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
__export(require("./pdf")); | ||
__export(require("./docraptor")); | ||
__export(require("./s3")); | ||
__exportStar(require("./pdf"), exports); | ||
__exportStar(require("./docraptor"), exports); | ||
__exportStar(require("./s3"), exports); |
@@ -81,2 +81,6 @@ /// <reference types="node" /> | ||
/** | ||
* PDF service version, defaults to v3. | ||
*/ | ||
version?: string; | ||
/** | ||
* Custom URL for the PDF service. | ||
@@ -83,0 +87,0 @@ */ |
115
lib/pdf.js
"use strict"; | ||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
return new (P || (P = Promise))(function (resolve, reject) { | ||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } | ||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } | ||
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); } | ||
step((generator = generator.apply(thisArg, _arguments || [])).next()); | ||
}); | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.GeneratedPdfResult = exports.PdfResult = exports.generatePdf = exports.setApiToken = void 0; | ||
const node_fetch_1 = require("node-fetch"); | ||
@@ -22,23 +15,21 @@ var globalToken = null; | ||
*/ | ||
function generatePdf(options) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const token = options.token || globalToken; | ||
if (token == null) { | ||
throw new Error('token is required'); | ||
} | ||
const url = options.serviceUrl || getUrl(options.region); | ||
const response = yield node_fetch_1.default(url, { | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
'Authorization': `Bearer ${token}` | ||
}, | ||
body: JSON.stringify(options) | ||
}); | ||
if (!response.ok) { | ||
throw new Error('Failed to generate PDF: ' + response.statusText + "\n" + (yield response.text())); | ||
} | ||
const { location } = yield response.json(); | ||
return new GeneratedPdfResult(location); | ||
async function generatePdf(options) { | ||
const token = options.token || globalToken; | ||
if (token == null) { | ||
throw new Error('token is required'); | ||
} | ||
const url = options.serviceUrl || getUrl(options); | ||
const response = await (0, node_fetch_1.default)(url, { | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
'Authorization': `Bearer ${token}` | ||
}, | ||
body: JSON.stringify(options) | ||
}); | ||
if (!response.ok) { | ||
throw new Error('Failed to generate PDF: ' + response.statusText + "\n" + await response.text()); | ||
} | ||
const { location } = await response.json(); | ||
return new GeneratedPdfResult(location); | ||
} | ||
@@ -62,7 +53,7 @@ exports.generatePdf = generatePdf; | ||
} | ||
function getUrl(region) { | ||
if (region == null) { | ||
region = autoRegion(); | ||
} | ||
return `https://pdf-${region}.journeyapps.com/v2/generate-pdf`; | ||
function getUrl(options) { | ||
var _a, _b; | ||
const region = (_a = options === null || options === void 0 ? void 0 : options.region) !== null && _a !== void 0 ? _a : autoRegion(); | ||
const version = (_b = options === null || options === void 0 ? void 0 : options.version) !== null && _b !== void 0 ? _b : 'v3'; | ||
return `https://pdf-${region}.journeyapps.com/${version}/generate-pdf`; | ||
} | ||
@@ -84,12 +75,10 @@ /** | ||
*/ | ||
toBuffer() { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
if (!this._buffer) { | ||
this._buffer = yield this.download(); | ||
if (!(this._buffer instanceof Buffer)) { | ||
throw new Error('Not a buffer!'); | ||
} | ||
async toBuffer() { | ||
if (!this._buffer) { | ||
this._buffer = await this.download(); | ||
if (!(this._buffer instanceof Buffer)) { | ||
throw new Error('Not a buffer!'); | ||
} | ||
return this._buffer; | ||
}); | ||
} | ||
return this._buffer; | ||
} | ||
@@ -99,6 +88,4 @@ /** | ||
*/ | ||
toBase64() { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
return (yield this.toBuffer()).toString('base64'); | ||
}); | ||
async toBase64() { | ||
return (await this.toBuffer()).toString('base64'); | ||
} | ||
@@ -110,16 +97,12 @@ /** | ||
*/ | ||
toEmailAttachment(name) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
return { | ||
content: yield this.toBase64(), | ||
filename: name, | ||
type: 'application/pdf', | ||
disposition: 'attachment' | ||
}; | ||
}); | ||
async toEmailAttachment(name) { | ||
return { | ||
content: await this.toBase64(), | ||
filename: name, | ||
type: 'application/pdf', | ||
disposition: 'attachment' | ||
}; | ||
} | ||
download() { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
throw new Error('Cannot download'); | ||
}); | ||
async download() { | ||
throw new Error('Cannot download'); | ||
} | ||
@@ -133,12 +116,10 @@ } | ||
} | ||
download() { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const pdfResponse = yield node_fetch_1.default(this.location); | ||
if (!pdfResponse.ok) { | ||
throw new Error(pdfResponse.statusText + ": " + (yield pdfResponse.text())); | ||
} | ||
return pdfResponse.buffer(); | ||
}); | ||
async download() { | ||
const pdfResponse = await (0, node_fetch_1.default)(this.location); | ||
if (!pdfResponse.ok) { | ||
throw new Error(pdfResponse.statusText + ": " + await pdfResponse.text()); | ||
} | ||
return pdfResponse.buffer(); | ||
} | ||
} | ||
exports.GeneratedPdfResult = GeneratedPdfResult; |
"use strict"; | ||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
return new (P || (P = Promise))(function (resolve, reject) { | ||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } | ||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } | ||
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); } | ||
step((generator = generator.apply(thisArg, _arguments || [])).next()); | ||
}); | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.S3UploadResult = exports.uploadToS3 = exports.generateAndUploadPdf = void 0; | ||
const pdf_1 = require("./pdf"); | ||
@@ -23,17 +16,15 @@ const node_fetch_1 = require("node-fetch"); | ||
*/ | ||
function generateAndUploadPdf(options, upload) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const AWS = require('aws-sdk'); | ||
const bucketName = upload.bucket; | ||
const credentials = upload.credentials; | ||
const s3 = new AWS.S3(credentials); | ||
const path = (upload.prefix || '') + upload.name; | ||
const url = s3.getSignedUrl('putObject', { | ||
Bucket: bucketName, | ||
Key: path, | ||
ContentType: 'application/pdf' | ||
}); | ||
const result = yield pdf_1.generatePdf(Object.assign({ uploadTo: url }, options)); | ||
return new S3UploadResult(path, upload); | ||
async function generateAndUploadPdf(options, upload) { | ||
const AWS = require('aws-sdk'); | ||
const bucketName = upload.bucket; | ||
const credentials = upload.credentials; | ||
const s3 = new AWS.S3(credentials); | ||
const path = (upload.prefix || '') + upload.name; | ||
const url = s3.getSignedUrl('putObject', { | ||
Bucket: bucketName, | ||
Key: path, | ||
ContentType: 'application/pdf' | ||
}); | ||
const result = await (0, pdf_1.generatePdf)({ uploadTo: url, ...options }); | ||
return new S3UploadResult(path, upload); | ||
} | ||
@@ -52,24 +43,22 @@ exports.generateAndUploadPdf = generateAndUploadPdf; | ||
*/ | ||
function uploadToS3(pdf, options) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const AWS = require('aws-sdk'); | ||
let buffer; | ||
if (pdf instanceof Buffer) { | ||
buffer = pdf; | ||
} | ||
else { | ||
buffer = yield pdf.toBuffer(); | ||
} | ||
const bucketName = options.bucket; | ||
const credentials = options.credentials; | ||
const s3 = new AWS.S3(credentials); | ||
const path = (options.prefix || '') + options.name; | ||
yield s3.putObject({ | ||
Bucket: options.bucket, | ||
Key: path, | ||
Body: buffer, | ||
ContentType: 'application/pdf' | ||
}).promise(); | ||
return new S3UploadResult(path, options, buffer); | ||
}); | ||
async function uploadToS3(pdf, options) { | ||
const AWS = require('aws-sdk'); | ||
let buffer; | ||
if (pdf instanceof Buffer) { | ||
buffer = pdf; | ||
} | ||
else { | ||
buffer = await pdf.toBuffer(); | ||
} | ||
const bucketName = options.bucket; | ||
const credentials = options.credentials; | ||
const s3 = new AWS.S3(credentials); | ||
const path = (options.prefix || '') + options.name; | ||
await s3.putObject({ | ||
Bucket: options.bucket, | ||
Key: path, | ||
Body: buffer, | ||
ContentType: 'application/pdf' | ||
}).promise(); | ||
return new S3UploadResult(path, options, buffer); | ||
} | ||
@@ -108,12 +97,10 @@ exports.uploadToS3 = uploadToS3; | ||
} | ||
download() { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const pdfResponse = yield node_fetch_1.default(this.getSignedUrl(300)); | ||
if (!pdfResponse.ok) { | ||
throw new Error(pdfResponse.statusText + ": " + (yield pdfResponse.text())); | ||
} | ||
return pdfResponse.buffer(); | ||
}); | ||
async download() { | ||
const pdfResponse = await (0, node_fetch_1.default)(this.getSignedUrl(300)); | ||
if (!pdfResponse.ok) { | ||
throw new Error(pdfResponse.statusText + ": " + await pdfResponse.text()); | ||
} | ||
return pdfResponse.buffer(); | ||
} | ||
} | ||
exports.S3UploadResult = S3UploadResult; |
{ | ||
"name": "@journeyapps/pdf-reports", | ||
"version": "0.4.0", | ||
"version": "1.0.0", | ||
"main": "lib/index.js", | ||
@@ -15,3 +15,3 @@ "repository": "https://github.com/journeyapps/pdf-reports", | ||
"aws-sdk": "^2.162.0", | ||
"node-fetch": "^1.7.3" | ||
"node-fetch": "^2.6.0" | ||
}, | ||
@@ -21,4 +21,4 @@ "devDependencies": { | ||
"@types/mocha": "^2.2.44", | ||
"@types/node": "^8.0.53", | ||
"@types/node-fetch": "^1.6.7", | ||
"@types/node": "^14.0.0", | ||
"@types/node-fetch": "^2.5.12", | ||
"aws-sdk": "^2.168.0", | ||
@@ -28,5 +28,5 @@ "chai": "^4.1.2", | ||
"node-fetch": "^1.7.3", | ||
"ts-node": "^4.0.1", | ||
"typescript": "^2.6.2" | ||
"ts-node": "^10.2.1", | ||
"typescript": "^4.4.3" | ||
} | ||
} |
@@ -134,2 +134,3 @@ # pdf-reports | ||
images with inline base64 content, are supported. | ||
* `-webkit-print-color-adjust:exact;` should be added to any styles that rely on background colors. | ||
@@ -136,0 +137,0 @@ ## Using DocRaptor |
@@ -18,3 +18,3 @@ import { expect } from 'chai'; | ||
expect(buffer.byteLength).to.gt(1000); | ||
expect(buffer.slice(0, 8).toString('utf-8')).to.eq('%PDF-1.4'); | ||
expect(buffer.slice(0, 7).toString('utf-8')).to.eq('%PDF-1.'); // %PDF-1.4 or %PDF-1.5 | ||
}).timeout(30000); | ||
@@ -33,4 +33,4 @@ | ||
expect(buffer.byteLength).to.gt(1000); | ||
expect(buffer.slice(0, 8).toString('utf-8')).to.eq('%PDF-1.4'); | ||
expect(buffer.slice(0, 7).toString('utf-8')).to.eq('%PDF-1.'); // %PDF-1.4 or %PDF-1.5 | ||
}).timeout(30000); | ||
}); |
@@ -20,3 +20,3 @@ import fetch from "node-fetch"; | ||
const url = options.serviceUrl || getUrl(options.region); | ||
const url = options.serviceUrl || getUrl(options); | ||
const response = await fetch(url, { | ||
@@ -53,7 +53,6 @@ method: 'POST', | ||
function getUrl(region?: string) { | ||
if (region == null) { | ||
region = autoRegion(); | ||
} | ||
return `https://pdf-${region}.journeyapps.com/v2/generate-pdf`; | ||
function getUrl(options?: {region?: string, version?: string}) { | ||
const region = options?.region ?? autoRegion(); | ||
const version = options?.version ?? 'v3'; | ||
return `https://pdf-${region}.journeyapps.com/${version}/generate-pdf`; | ||
} | ||
@@ -175,2 +174,7 @@ | ||
/** | ||
* PDF service version, defaults to v3. | ||
*/ | ||
version?: string; | ||
/** | ||
* Custom URL for the PDF service. | ||
@@ -177,0 +181,0 @@ */ |
@@ -5,4 +5,4 @@ { | ||
"module": "commonjs", | ||
"target": "es2015", | ||
"lib": ["es2016"], | ||
"target": "es2018", | ||
"lib": ["es2018"], | ||
"noImplicitAny": true, | ||
@@ -16,2 +16,3 @@ "preserveConstEnums": true, | ||
"allowJs": false, | ||
"composite": true, | ||
"types": [ | ||
@@ -18,0 +19,0 @@ "node", |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
1
185
46772
23
953