Skip to content

Commit

Permalink
first version
Browse files Browse the repository at this point in the history
  • Loading branch information
Trey Smith committed Sep 16, 2021
1 parent 294d9ac commit d108484
Show file tree
Hide file tree
Showing 21 changed files with 819 additions and 441 deletions.
193 changes: 29 additions & 164 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
pdf2png.js
Pdf2PNG (Typescript Support)
============

Install:
npm install pdf2png-mp
```
npm install pdf2png-ts
```


This is fork another project, that can convert many-page .pdf file. (https://github.com/thnew/Pdf2Png)
This is based on another project. (https://github.com/Inkognitoo/Pdf2Png)

This version uses typescript and promises to greatly simplify usage since the old module is almost 7 years old.

This project uses ghostscript, but there's no need to install it (if you use windows).
If you want the module to use a local installation of ghostscript, set the option useLocalGhostscript true.
Expand All @@ -18,169 +23,29 @@ http://www.ghostscript.com/

here some examples how to use:

```javascript
// Many page example
pdf2png.convert("./example.pdf", { quality: 300 }, function(resp){
if(!resp.success)
{
console.log("Something went wrong: " + resp.error);

return;
}

console.log("Yayy the pdf got converted, now I'm gonna save it!");

var fs = require('fs');

resp.data.forEach(function(item, index) {

fs.writeFile("./example_simple"+index+".png", item, function (err) {
if (err) {
console.log(err);
}
else {
console.log("The file "+index+" was saved!");
}
});
});
});

// Most simple example
pdf2png.convert(__dirname + "/example.pdf", function(resp){
if(!resp.success)
{
console.log("Something went wrong: " + resp.error);

return;
}

console.log("Yayy the pdf got converted, now I'm gonna save it!");

fs.writeFile("test/example_simple.png", resp.data, function(err) {
if(err) {
console.log(err);
}
else {
console.log("The file was saved!");
}
});
});
```typescript

// Example that returns a path
pdf2png.convert(__dirname + "/example.pdf", { returnFilePath: true }, function(resp){
if(!resp.success)
{
console.log("Something went wrong: " + resp.error);

return;
}

console.log("Yayy the pdf got converted, now I'm gonna save it!");

var img = fs.readFileSync(resp.data);

fs.writeFile("test/example_that_returns_a_path.png", img, function(err) {
if(err) {
console.log(err);
}
else {
console.log("The file was saved!");
}
});
});
// Create a PDFConvert Object to use for the each pdf file
// Buffer
const pdfConverter = new PDFConvert(buffer);
// OR web url
const pdfConverter = new PDFConvert("http://example.com/example.pdf");

// Example with lower quality
pdf2png.convert(__dirname + "/example.pdf", { quality: 50 }, function(resp){
if(!resp.success)
{
console.log("Something went wrong: " + resp.error);

return;
}

console.log("Yayy the pdf got converted, now I'm gonna save it!");

fs.writeFile("test/example_with_lower_quality.png", resp.data, function(err) {
if(err) {
console.log(err);
}
else {
console.log("The file was saved!");
}
});
});

// Example with higher quality
pdf2png.convert(__dirname + "/example.pdf", { quality: 200 }, function(resp){
if(!resp.success)
{
console.log("Something went wrong: " + resp.error);

return;
}

console.log("Yayy the pdf got converted, now I'm gonna save it!");

fs.writeFile("test/example_with_higher_quality.png", resp.data, function(err) {
if(err) {
console.log(err);
}
else {
console.log("The file was saved!");
}
});
});

// Example using a local ghostscript installation
pdf2png.convert(__dirname + "/example.pdf", { useLocalGhostscript: true }, function(resp){
if(!resp.success)
{
console.log("Something went wrong: " + resp.error);

return;
}

console.log("Yayy the pdf got converted, now I'm gonna save it!");

fs.writeFile("test/example_simple.png", resp.data, function(err) {
if(err) {
console.log(err);
}
else {
console.log("The file was saved!");
}
});
});
```

If an error like this appears:
Something went wrong: Error converting pdf to png: Error: Command failed: 'gs' is not recognized as an internal or external command, operable program or batch file.

Maybe you have the node file you execute in a subfolder and Pdf2Png doesn't set the path to ghostscript correctly anymore.
You can rewrite the path to the executable by setting "pdf2png.ghostscriptPath".
Look at the following example of a script, being in the subfolder /lib.
It first detects the project-root folder and then builds the absolute path to the ghostscript folder.

```javascript
var projectPath = __dirname.split("\\");
projectPath.pop();
projectPath = projectPath.join("\\");

var gsPath = projectPath + "\\executables\\ghostScript";

// Rewrite the ghostscript path
pdf2png.ghostscriptPath = gsPath;
```
// Get the page count of the PDF (this returns a promise)
const pages = await pdfConverter.getPageCount()

Options:
bool useLocalGhostscript
If true, the moudle won't set an envirponment attribute to the ghostscript executable.
Set this true if you want to use an own local ghostscript installation
// Get page 1 as a PNG Image Buffer
const buffer = await pdfConverter.convertPageToImage(1)
await fs.writeFile("example_page1.png", buffer);

bool returnFilePath
If you set this true, the module won't return you file-data, it will return you a path to a temporary file instead, containing the image.
Don't forget to remove this temporary file.
/**
* Make sure you always clean the converter object when you're done using it,
* Otherwise the pdf tmp file will not be removed.
*
* Having to manually call clean allows you to run multiple operations on the same tmp file
* and reduces latency with having to refetch and write the pdf to a tmp location on every
* call.
*/
pdfConverter.clean()

int quality [ = 100]
The quality of the PNG
Can be higher and lower, play with it
```
28 changes: 28 additions & 0 deletions dist/pdf2png.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/// <reference types="node" />
export declare const ghostscriptPath: string;
export default class PDFConvert {
options: any;
source: Buffer | string;
tempLocation: string | undefined;
/**
* Constructs a new Convert Object
* @param source Can be either the buffer of the data, or a web url of the file
* @param options ghostScript Options
*/
constructor(source: Buffer | string, options?: any);
/**
* Gets the page count of the pdf
* @returns number of pages in the pdf
*/
getPageCount: () => Promise<number>;
/**
* Writes the source file to a tmp location
* @returns Filename
*/
private writeFileToTemp;
/**
* Removes the temp file created for the PDF conversion
* This should be called manually in case you want to do multiple operations
*/
clean: () => Promise<void>;
}
107 changes: 107 additions & 0 deletions dist/pdf2png.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions dist/pdf2png.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 29 additions & 0 deletions dist/src/pdf2png.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/// <reference types="node" />
export default class PDFConvert {
options: any;
source: Buffer | string;
tempLocation: string | undefined;
ghostscriptPath: string;
/**
* Constructs a new Convert Object
* @param source Can be either the buffer of the data, or a web url of the file
* @param options ghostScript Options
*/
constructor(source: Buffer | string, options?: any);
convertPageToImage: (page: number) => Promise<Buffer>;
/**
* Gets the page count of the pdf
* @returns number of pages in the pdf
*/
getPageCount: () => Promise<number>;
/**
* Writes the source file to a tmp location
* @returns Filename
*/
private writePDFToTemp;
/**
* Removes the temp file created for the PDF conversion
* This should be called manually in case you want to do multiple operations
*/
clean: () => Promise<void>;
}
Loading

0 comments on commit d108484

Please sign in to comment.