Skip to content

Commit

Permalink
changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Trey Smith committed Sep 20, 2021
1 parent 4484c7a commit 054ed21
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 53 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "pdf2png-ts",
"version": "0.0.8",
"version": "0.0.12",
"description": "Takes a PDF-document and converts and delivers a PNG (Typescript Support and simplified dependencies)",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
58 changes: 33 additions & 25 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import path from "path";
import tmp from "tmp";
import axios from "axios";
import { readFile, unlink, writeFile } from "fs/promises";
import { createWriteStream } from "fs";



Expand Down Expand Up @@ -96,38 +97,45 @@ export default class PDFConvert {

let file: Buffer;

// If this is a web path then use axios to fetch the file
if(typeof(this.source) === "string") {

try {
tmp.file({}, async (err, name, fd) => {

const response = await axios({
url: this.source,
method: 'GET',
responseType: 'blob'
})
if(err)
return reject(`Unable to open tmp file: ` + err);

file = response.data;
// If this is a web path then use axios to fetch the file
if(typeof(this.source) === "string") {

try {

} catch (e) {
return reject("Unable to fetch file from web location: " + e);
}//end try catch
const response = await axios({
url: this.source,
method: 'GET',
responseType: 'stream'
})

} else {
file = this.source;
}//end if else
// const createWriteStream(name);
// file = response.data;

tmp.file({}, async (err, name, fd) => {
const stream = createWriteStream(name)
response.data.pipe(stream);
stream.on('finish', () => {
return resolve(name);
})

if(err)
return reject(`Unable to open tmp file: ` + err);
} catch (e) {
return reject("Unable to fetch file from web location: " + e);
}//end try catch

try {
await writeFile(name, file);
return resolve(name);
} catch (e) {
return reject(`Unable to write tmp file: ` + e);
}//end try catch
} else {

try {
await writeFile(name, file);
return resolve(name);
} catch (e) {
return reject(`Unable to write tmp file: ` + e);
}//end try catch

}//end if else

})

Expand Down
24 changes: 24 additions & 0 deletions test/example.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const { readFileSync, writeFileSync } = require("fs");
const PDFConvert = require ("../dist/index.js").default;


// const buffer = readFileSync("multipage.pdf");

const pdfConverter = new PDFConvert("https://acplrollandcenterblob.blob.core.windows.net/cms-assets/436dd470-b1cf-11eb-936b-75a3a36a6a90.pdf");

pdfConverter.getPageCount()
.then(pages => {
console.log("PDF page count: ", pages);

// pdfConverter.convertPageToImage(1)
// .then(buffer => {
// writeFileSync("example.png", buffer);
// console.log("PDF Written to file as PNG Image")
// pdfConverter.clean();
// })

})
.catch(error => {
console.error(error);
pdfConverter.clean();
})
Empty file added test/example.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
24 changes: 0 additions & 24 deletions test/example.ts

This file was deleted.

4 changes: 1 addition & 3 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,13 @@
"compilerOptions": {
"declaration": true,
"strictNullChecks": true,
"esModuleInterop":true,
"target": "es6",
"module": "commonjs",
"outDir": "dist",
"strict": true,
"skipLibCheck": true,
"esModuleInterop": true,
"moduleResolution": "node",
"sourceMap": true,
"lib": ["ES2016"]
},
"include": ["src"],
"exclude": ["node_modules", "dist"]
Expand Down

0 comments on commit 054ed21

Please sign in to comment.