Skip to content

Commit

Permalink
allow user to cancel pdf password prompt
Browse files Browse the repository at this point in the history
fixes #120
  • Loading branch information
ananthakumaran committed Dec 30, 2023
1 parent 87449db commit 9cdb168
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 11 deletions.
9 changes: 7 additions & 2 deletions src/lib/pdf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,14 @@ function makeRow(cells: TextItemWithPosition[]): string[] {
* @param data
*/
export async function pdf2array(data: ArrayBuffer): Promise<string[][]> {
const loader = await pdfjs.getDocument(data);
const loader = pdfjs.getDocument(data);
loader.onPassword = (cb: any) => {
const password = prompt("Please enter the password to open this PDF file.");
const password = prompt(
"Please enter the password to open this PDF file. Press cancel to exit."
);
if (!password) {
throw new Error("Password required.");
}
cb(password);
};
const doc = await loader.promise;
Expand Down
11 changes: 8 additions & 3 deletions src/lib/sheet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { pdf2array } from "./pdf";

interface Result {
data: string[][];
error?: string;
}

export function parse(file: File): Promise<Result> {
Expand Down Expand Up @@ -84,9 +85,13 @@ async function parseXLSX(file: File): Promise<Result> {
}

async function parsePDF(file: File): Promise<Result> {
const buffer = await readFile(file);
const array = await pdf2array(buffer);
return { data: array };
try {
const buffer = await readFile(file);
const array = await pdf2array(buffer);
return { data: array };
} catch (e) {
return { data: [], error: e.message };
}
}

function readFile(file: File): Promise<ArrayBuffer> {
Expand Down
24 changes: 18 additions & 6 deletions src/routes/(app)/ledger/import/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
let lastTemplate: any;
let lastData: any;
let preview = "";
let parseErrorMessage: string = null;
let columnCount: number;
let data: any[][] = [];
let rows: Array<Record<string, any>> = [];
Expand Down Expand Up @@ -114,13 +115,18 @@
const { acceptedFiles } = e.detail;
const results = await parse(acceptedFiles[0]);
data = results.data;
rows = asRows(results);
if (results.error) {
parseErrorMessage = results.error;
} else {
parseErrorMessage = null;
data = results.data;
rows = asRows(results);
columnCount = _.maxBy(data, (row) => row.length).length;
_.each(data, (row) => {
row.length = columnCount;
});
columnCount = _.maxBy(data, (row) => row.length).length;
_.each(data, (row) => {
row.length = columnCount;
});
}
}
async function copyToClipboard() {
Expand Down Expand Up @@ -303,6 +309,12 @@
<label for="import-reverse">Reverse</label>
</div>
</div>
{#if parseErrorMessage}
<div class="message invertable is-danger">
<div class="message-header">Failed to parse document</div>
<div class="message-body">{parseErrorMessage}</div>
</div>
{/if}
{#if !_.isEmpty(data)}
<div class="table-wrapper">
<table
Expand Down

0 comments on commit 9cdb168

Please sign in to comment.