Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cross platform build and exec #4

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
3 changes: 2 additions & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [

{
"name": "Test Doctor",
"type": "node",
"request": "launch",
"program": "${workspaceFolder}/index.js",
"program": "${workspaceFolder}/dist/index.js",
"args": ["doctor"]
},
{
Expand Down
54 changes: 54 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# Contributing to pdfstamp
We love your input! We want to make contributing to this project as easy and transparent as possible, whether it's:

- Reporting a bug
- Discussing the current state of the code
- Submitting a fix
- Proposing new features
- Becoming a maintainer

## We Develop with Github
We use Github to host code, to track issues and feature requests, as well as accept pull requests.

## We Use [Github Flow](https://guides.github.com/introduction/flow/index.html), So All Code Changes Happen Through Pull Requests
Pull requests are the best way to propose changes to the codebase (we use [Github Flow](https://guides.github.com/introduction/flow/index.html)). We actively welcome your pull requests:

1. Fork the repo and create your branch from `master`.
2. If you've added code that should be tested, add tests.
3. If you've changed APIs, update the documentation.
4. Ensure the test suite passes.
5. Make sure your code lints.
6. Issue that pull request!

## Any contributions you make will be under the MIT Software License
In short, when you submit code changes, your submissions are understood to be under the same [MIT License](http://choosealicense.com/licenses/mit/) that covers the project. Feel free to contact the maintainers if that's a concern.

## Report bugs using Github's [issues](https://github.com/mavaddat/pdfstamp/issues)
We use GitHub issues to track public bugs. Report a bug by [opening a new issue](); it's that easy!

## Write bug reports with detail, background, and sample code
[This is an example](http://stackoverflow.com/q/12488905/180626) of a bug report I wrote, and I think it's not a bad model. Here's [another example from Craig Hockenberry](http://www.openradar.me/11905408), an app developer whom I greatly respect.

**Great Bug Reports** tend to have:

- A quick summary and/or background
- Steps to reproduce
- Be specific!
- Give sample code if you can. [My StackOverflow question](http://stackoverflow.com/q/12488905/180626) includes sample code that *anyone* with a base R setup can run to reproduce what I was seeing
- What you expected would happen
- What actually happens
- Notes (possibly including why you think this might be happening, or stuff you tried that didn't work)

People *love* thorough bug reports. I'm not even kidding.

## Use a Consistent Coding Style
I'm again borrowing these from [Facebook's Guidelines](https://github.com/facebook/draft-js/blob/main/CONTRIBUTING.md)

* 2 spaces for indentation rather than tabs
* You can try running `npm run lint` for style unification

## License
By contributing, you agree that your contributions will be licensed under its MIT License.

## References
This document was adapted from the open-source contribution guidelines for [Facebook's Draft](https://github.com/facebook/draft-js/blob/main/CONTRIBUTING.md)
20 changes: 10 additions & 10 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@

import program from "commander";
import chalk from "chalk";
import os from "os";
import os, { type } from "os";
import path from "path";
import rimraf from "rimraf";
import sh from "shelljs";
import { execCmd, execCmdResult } from "./utils/exec";
import { execCmd, execCmdResult } from "./utils/exec"
import { CalculateZoom, CalculateOrientation } from "./utils/signature-utils";

var IS_DEBUG = false;
var IS_DEBUG = true;
function log(...args: any[]) {
IS_DEBUG && console.log('DEBUG: ', ...args);
IS_DEBUG && console.debug(...args);
}

async function exists(commandName: string, installTxt: string) {
Expand Down Expand Up @@ -178,7 +178,7 @@ export async function stamp({
);
// Combine to original pdf
const joinDash = (a: string, b: string): string => {
return [a, b].filter((a) => !!a).join("-");
return [a, b].filter((e) => !!e).join("-");
}
const start1 = PAGE_NUM == 1 ? "" : "A1";
const start2 = PAGE_NUM <= 2 ? "" : `${PAGE_NUM - 1}`;
Expand All @@ -197,33 +197,33 @@ export async function stamp({
if (IS_DEBUG) {
const debugDir = './_pdf-stamp-temp';
sh.mkdir('-p', debugDir);
await execCmd(`mv ${TempFiles.map(f => `"${f}"`).join(' ')} ${debugDir}`)
await execCmd(`npx shx mv ${TempFiles.map(f => '"'+f+'"').join(' ')} ${debugDir}`)
}

await Promise.all(TempFiles.map(f => RemoveFile(f)));
}

async function NormaliseSignatureGetPath(inputSignaturePath: string, outputPath: string, width: number) {
await execCmd(`convert ${inputSignaturePath} -set colorspace sRGB -resize '${width}x${width}' "${outputPath}"`)
await execCmd(`convert ${inputSignaturePath} -set colorspace sRGB -resize ${width}x${width} "${outputPath}"`)
}

function GetPdfDataString(inputPdfPath: string) {
const res = sh
.exec(`pdftk "${inputPdfPath}" dump_data`, { silent: true })
.toString();
if (!res.includes("NumberOfPages")) {
throw `There was a problem reading the input PDF "${inputPdfPath}"`;
throw new Error(`There was a problem reading the input PDF "${inputPdfPath}"`);
}
return res;
}

function GetPageCount(pdfDataDump: string, pageNum: number) {
const pageCount = +(pdfDataDump?.split("NumberOfPages: ")?.pop()?.split("\n")?.shift() || '');
if (pageNum > pageCount) {
throw "--page must be <= the number of pages in the input document";
throw new Error("--page must be <= the number of pages in the input document");
}
if (pageNum < 1) {
throw "--page must be > 0";
throw new Error("--page must be > 0");
}
return pageCount;
}
Expand Down
Loading