Skip to content
This repository has been archived by the owner on Apr 15, 2020. It is now read-only.

Option to omit page number in output file name #25

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ pdfImage.convertPage(0).then(function (imagePath) {
fs.existsSync("/tmp/slide-0.png") // => true
});
```
> Note: If you do NOT want page numbers in your image file names, use option "omitPageNumOnFileName"
```
pdfImage.convertPage(0, {omitPageNumOnFileName:true}).then()
```

## Express

Expand Down
15 changes: 8 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,11 @@ PDFImage.prototype = {
return info["Pages"];
});
},
getOutputImagePathForPage: function (pageNumber) {
/** @options{omitPageNumOnFileName:Boolean} */
getOutputImagePathForPage: function (pageNumber, options) {
return path.join(
this.outputDirectory,
this.pdfFileBaseName + "-" + pageNumber + "." + this.convertExtension
this.pdfFileBaseName + (!options||!options.omitPageNumOnFileName?"-"+pageNumber:"") + "." + this.convertExtension
);
},
setConvertOptions: function (convertOptions) {
Expand All @@ -71,9 +72,9 @@ PDFImage.prototype = {
setConvertExtension: function (convertExtension) {
this.convertExtension = convertExtension || "png";
},
constructConvertCommandForPage: function (pageNumber) {
constructConvertCommandForPage: function (pageNumber, options) {
var pdfFilePath = this.pdfFilePath;
var outputImagePath = this.getOutputImagePathForPage(pageNumber);
var outputImagePath = this.getOutputImagePathForPage(pageNumber, options);
var convertOptionsString = this.constructConvertOptions();
return util.format(
"%s %s'%s[%d]' '%s'",
Expand All @@ -91,10 +92,10 @@ PDFImage.prototype = {
}
}, this).join(" ");
},
convertPage: function (pageNumber) {
convertPage: function (pageNumber, options) {
var pdfFilePath = this.pdfFilePath;
var outputImagePath = this.getOutputImagePathForPage(pageNumber);
var convertCommand = this.constructConvertCommandForPage(pageNumber);
var outputImagePath = this.getOutputImagePathForPage(pageNumber, options);
var convertCommand = this.constructConvertCommandForPage(pageNumber, options);

var promise = new Promise(function (resolve, reject) {
function convertPageToImage() {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "pdf-image",
"version": "1.0.2",
"version": "1.0.3",
"main": "index.js",
"repository": {
"type": "git",
Expand Down
2 changes: 2 additions & 0 deletions tests/test-main.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ describe("PDFImage", function () {
.equal("/tmp/test-2.png");
expect(pdfImage.getOutputImagePathForPage(1000))
.equal("/tmp/test-1000.png");
expect(pdfImage.getOutputImagePathForPage(1000,{omitPageNumOnFileName:true}))
.equal("/tmp/test.png");
});

it("should return correct convert command", function () {
Expand Down