Skip to content

#1 PDF Application on GitHub that lets you edit PDFs on any device anywhere

Keep the file name when saving in the PDF Text Editor (#8882)

Anthony Stirling157b10c7d941
diff --git a/frontend/src/editor/core/tests/stubbed/pdf-text-editor-protected-and-save-warning.spec.ts b/frontend/src/editor/core/tests/stubbed/pdf-text-editor-protected-and-save-warning.spec.ts
index cc961e7838..4dd82b0f4f 100644
--- a/frontend/src/editor/core/tests/stubbed/pdf-text-editor-protected-and-save-warning.spec.ts
+++ b/frontend/src/editor/core/tests/stubbed/pdf-text-editor-protected-and-save-warning.spec.ts
@@ -75,7 +75,7 @@ test.describe("PDF text editor - encrypted PDF password prompt", () => {
const downloadPromise = page.waitForEvent("download");
await page.getByTestId("pdf-editor-save-risk-confirm").click();
const download = await downloadPromise;
- expect(download.suggestedFilename()).toBe("encrypted_edited.pdf");
+ expect(download.suggestedFilename()).toBe("encrypted.pdf");
const savedPath = test.info().outputPath("encrypted_edited.pdf");
await download.saveAs(savedPath);
const source = await readPdfProtection(
diff --git a/frontend/src/editor/core/tests/stubbed/pdf-text-editor-workbench-save.spec.ts b/frontend/src/editor/core/tests/stubbed/pdf-text-editor-workbench-save.spec.ts
index 019515df5f..24cb77bb6e 100644
--- a/frontend/src/editor/core/tests/stubbed/pdf-text-editor-workbench-save.spec.ts
+++ b/frontend/src/editor/core/tests/stubbed/pdf-text-editor-workbench-save.spec.ts
@@ -26,31 +26,40 @@ async function openEditorWithSample(page: Page) {
}
/** Type into the first text run on page 0 so the document is genuinely dirty. */
-async function editFirstRun(page: Page) {
+async function editFirstRun(page: Page, text = "ZZSAVED") {
const firstRun = page.locator('[data-testid^="pdf-editor-run-p0-"]').first();
await expect(firstRun).toBeVisible({ timeout: 30_000 });
const id = (await firstRun.getAttribute("data-testid")) ?? "";
- await page.evaluate((testId) => {
- const el = document.querySelector<HTMLElement>(`[data-testid="${testId}"]`);
- if (!el) throw new Error("run not found");
- el.focus();
- const range = document.createRange();
- range.selectNodeContents(el);
- range.collapse(false);
- const sel = window.getSelection();
- sel?.removeAllRanges();
- sel?.addRange(range);
- document.execCommand("insertText", false, "ZZSAVED");
- }, id);
- await expect(firstRun).toContainText("ZZSAVED");
+ await page.evaluate(
+ ([testId, inserted]) => {
+ const el = document.querySelector<HTMLElement>(
+ `[data-testid="${testId}"]`,
+ );
+ if (!el) throw new Error("run not found");
+ el.focus();
+ const range = document.createRange();
+ range.selectNodeContents(el);
+ range.collapse(false);
+ const sel = window.getSelection();
+ sel?.removeAllRanges();
+ sel?.addRange(range);
+ document.execCommand("insertText", false, inserted);
+ },
+ [id, text],
+ );
+ await expect(firstRun).toContainText(text);
await expect(page.getByTestId("pdf-editor-dirty-dot")).toBeVisible();
}
/** The workbench view switcher's "Active Files" tab (a Mantine radio label). */
function activeFilesTab(page: Page) {
+ return workbenchTab(page, /^Active Files$/);
+}
+
+function workbenchTab(page: Page, name: RegExp) {
return page
.locator(".workbench-bar-views label")
- .filter({ hasText: /^Active Files$/ })
+ .filter({ hasText: name })
.first();
}
@@ -144,6 +153,34 @@ test.describe("PDF text editor - standardised save", () => {
).toHaveText("v2", { timeout: 30_000 });
});
+ test("every save lands on the same file under its own name", async ({
+ page,
+ }) => {
+ test.setTimeout(180_000);
+ await openEditorWithSample(page);
+ const cards = page.getByTestId("file-thumbnail");
+
+ for (const [text, version] of [
+ ["ZZFIRST", "v2"],
+ ["ZZSECOND", "v3"],
+ ]) {
+ await workbenchTab(page, /^Editor$/).click();
+ await editFirstRun(page, text);
+ await page.getByTestId("pdf-editor-save").click();
+ await expect(page.getByTestId("pdf-editor-dirty-dot")).toBeHidden({
+ timeout: 60_000,
+ });
+
+ await activeFilesTab(page).click();
+ await expect(cards).toHaveCount(1);
+ await expect(cards.first().getByTestId("file-version-badge")).toHaveText(
+ version,
+ { timeout: 30_000 },
+ );
+ await expect(cards.first()).toContainText("sample.pdf");
+ }
+ });
+
test("cancelling the download still keeps the saved edit", async ({
page,
}) => {
diff --git a/frontend/src/editor/core/tools/pdfTextEditor/PdfTextEditor.tsx b/frontend/src/editor/core/tools/pdfTextEditor/PdfTextEditor.tsx
index 0618c25aed..c15e21a422 100644
--- a/frontend/src/editor/core/tools/pdfTextEditor/PdfTextEditor.tsx
+++ b/frontend/src/editor/core/tools/pdfTextEditor/PdfTextEditor.tsx
@@ -258,10 +258,8 @@ export default function PdfTextEditor(_props: BaseToolProps) {
// The position that is about to be written out. Anything the user edits
// while the export runs is NOT in these bytes, so it must stay dirty.
const exported = store.savedPosition();
- const { blob, filename } = await exportToBlob(
- store.document,
- openedFileName,
- );
+ const blob = await exportToBlob(store.document);
+ const filename = openedFileName?.trim() || "edited.pdf";
// Apply first and unconditionally. Gating the write-back on the browser
// download dialog meant cancelling it silently discarded the save.
assertFilesNotBlocked([sourceId ?? undefined]);
diff --git a/frontend/src/editor/core/tools/pdfTextEditor/__tests__/encryptedExport.test.ts b/frontend/src/editor/core/tools/pdfTextEditor/__tests__/encryptedExport.test.ts
index dff4cb468c..2e26f35627 100644
--- a/frontend/src/editor/core/tools/pdfTextEditor/__tests__/encryptedExport.test.ts
+++ b/frontend/src/editor/core/tools/pdfTextEditor/__tests__/encryptedExport.test.ts
@@ -39,7 +39,7 @@ async function open(bytes: Uint8Array, password: string) {
}
async function savedBytes(doc: EditorDocument) {
- const { blob } = await exportToBlob(doc, "encrypted.pdf");
+ const blob = await exportToBlob(doc);
return new Uint8Array(await blob.arrayBuffer());
}
diff --git a/frontend/src/editor/core/tools/pdfTextEditor/__tests__/innerTags.test.ts b/frontend/src/editor/core/tools/pdfTextEditor/__tests__/innerTags.test.ts
index 6b75365837..24f53f26de 100644
--- a/frontend/src/editor/core/tools/pdfTextEditor/__tests__/innerTags.test.ts
+++ b/frontend/src/editor/core/tools/pdfTextEditor/__tests__/innerTags.test.ts
@@ -122,7 +122,7 @@ async function savedIn(
}
async function saved(doc: EditorDocument) {
- const { blob } = await exportToBlob(doc, "tags.pdf");
+ const blob = await exportToBlob(doc);
const bytes = new Uint8Array(await blob.arrayBuffer());
return { bytes, structure: (await readFileStructure(bytes))! };
}
diff --git a/frontend/src/editor/core/tools/pdfTextEditor/__tests__/languageTagging.test.ts b/frontend/src/editor/core/tools/pdfTextEditor/__tests__/languageTagging.test.ts
index 4136fb9db8..6bcc0f1611 100644
--- a/frontend/src/editor/core/tools/pdfTextEditor/__tests__/languageTagging.test.ts
+++ b/frontend/src/editor/core/tools/pdfTextEditor/__tests__/languageTagging.test.ts
@@ -182,7 +182,7 @@ function mcidOf(doc: EditorDocument, page: number, fragment: string): number {
}
async function savedBytes(doc: EditorDocument): Promise<Uint8Array> {
- const { blob } = await exportToBlob(doc, "letter.pdf");
+ const blob = await exportToBlob(doc);
return new Uint8Array(await blob.arrayBuffer());
}
diff --git a/frontend/src/editor/core/tools/pdfTextEditor/__tests__/linkTagging.test.ts b/frontend/src/editor/core/tools/pdfTextEditor/__tests__/linkTagging.test.ts
index dff2337618..2cd0d0346e 100644
--- a/frontend/src/editor/core/tools/pdfTextEditor/__tests__/linkTagging.test.ts
+++ b/frontend/src/editor/core/tools/pdfTextEditor/__tests__/linkTagging.test.ts
@@ -137,7 +137,7 @@ async function open(bytes: Uint8Array): Promise<EditorDocument> {
}
async function savedBytes(doc: EditorDocument): Promise<Uint8Array> {
- const { blob } = await exportToBlob(doc, "links.pdf");
+ const blob = await exportToBlob(doc);
return new Uint8Array(await blob.arrayBuffer());
}
diff --git a/frontend/src/editor/core/tools/pdfTextEditor/__tests__/repairStructure.test.ts b/frontend/src/editor/core/tools/pdfTextEditor/__tests__/repairStructure.test.ts
index a891718036..aff1c501fa 100644
--- a/frontend/src/editor/core/tools/pdfTextEditor/__tests__/repairStructure.test.ts
+++ b/frontend/src/editor/core/tools/pdfTextEditor/__tests__/repairStructure.test.ts
@@ -378,7 +378,7 @@ describe("repairStructure on the editor's own output", () => {
page.markDirty();
page.markNeedsGenerate();
- const { blob } = await exportToBlob(doc, "sample.pdf");
+ const blob = await exportToBlob(doc);
const pdf = (await RawPdf.parse(new Uint8Array(await blob.arrayBuffer())))!;
const mcids = await contentMcids(pdf);
expect(new Set(mcids).size).toBe(mcids.length);
@@ -466,7 +466,7 @@ describe("repairStructure on the editor's own output", () => {
page.markDirty();
page.markNeedsGenerate();
- const { blob } = await exportToBlob(doc, "named.pdf");
+ const blob = await exportToBlob(doc);
const pdf = (await RawPdf.parse(new Uint8Array(await blob.arrayBuffer())))!;
// PDFium writes the untouched sequences back in named form.
expect(await contentText(pdf)).toContain("/MC1 BDC");
diff --git a/frontend/src/editor/core/tools/pdfTextEditor/__tests__/structureEditing.test.ts b/frontend/src/editor/core/tools/pdfTextEditor/__tests__/structureEditing.test.ts
index 66d288ecfc..2a9b20f04b 100644
--- a/frontend/src/editor/core/tools/pdfTextEditor/__tests__/structureEditing.test.ts
+++ b/frontend/src/editor/core/tools/pdfTextEditor/__tests__/structureEditing.test.ts
@@ -121,7 +121,7 @@ async function openSample(): Promise<EditorDocument> {
}
async function savedBytes(doc: EditorDocument): Promise<Uint8Array> {
- const { blob } = await exportToBlob(doc, "sample.pdf");
+ const blob = await exportToBlob(doc);
return new Uint8Array(await blob.arrayBuffer());
}
@@ -947,7 +947,7 @@ describe("saving a description", () => {
]);
// The detail is logged for a bug report; the user gets a message they can act on.
expectConsole.error(/Could not write 1 tag edit/);
- const error = await exportToBlob(doc, "sample.pdf").catch((e) => e);
+ const error = await exportToBlob(doc).catch((e) => e);
expect(error.message).toBe(
"Your accessibility changes couldn't be saved, so the file wasn't saved. Undo them and try again.",
);
diff --git a/frontend/src/editor/core/tools/pdfTextEditor/__tests__/structureGrouping.test.ts b/frontend/src/editor/core/tools/pdfTextEditor/__tests__/structureGrouping.test.ts
index 1f28a61358..8775f02feb 100644
--- a/frontend/src/editor/core/tools/pdfTextEditor/__tests__/structureGrouping.test.ts
+++ b/frontend/src/editor/core/tools/pdfTextEditor/__tests__/structureGrouping.test.ts
@@ -426,7 +426,7 @@ describe("Chrome-style tagging", () => {
}),
doc,
);
- const { blob } = await exportToBlob(doc, "chrome.pdf");
+ const blob = await exportToBlob(doc);
const pdf = (await RawPdf.parse(new Uint8Array(await blob.arrayBuffer())))!;
expect(pdf.dictName(pdf.objectBody(8)!, "S")).toBe("H2");
expect(pdf.dictName(pdf.objectBody(9)!, "S")).toBe("NonStruct");
diff --git a/frontend/src/editor/core/tools/pdfTextEditor/__tests__/tagPageContent.test.ts b/frontend/src/editor/core/tools/pdfTextEditor/__tests__/tagPageContent.test.ts
index c98ba85cbe..8632718847 100644
--- a/frontend/src/editor/core/tools/pdfTextEditor/__tests__/tagPageContent.test.ts
+++ b/frontend/src/editor/core/tools/pdfTextEditor/__tests__/tagPageContent.test.ts
@@ -132,7 +132,7 @@ async function open(bytes: Uint8Array): Promise<EditorDocument> {
}
async function savedBytes(doc: EditorDocument): Promise<Uint8Array> {
- const { blob } = await exportToBlob(doc, "page.pdf");
+ const blob = await exportToBlob(doc);
return new Uint8Array(await blob.arrayBuffer());
}
diff --git a/frontend/src/editor/core/tools/pdfTextEditor/__tests__/textRangeTagging.test.ts b/frontend/src/editor/core/tools/pdfTextEditor/__tests__/textRangeTagging.test.ts
index d1b4707f61..38d0294f18 100644
--- a/frontend/src/editor/core/tools/pdfTextEditor/__tests__/textRangeTagging.test.ts
+++ b/frontend/src/editor/core/tools/pdfTextEditor/__tests__/textRangeTagging.test.ts
@@ -268,7 +268,7 @@ function runTexts(doc: EditorDocument, pageIndex = 0): string[] {
}
async function savedBytes(doc: EditorDocument): Promise<Uint8Array> {
- const { blob } = await exportToBlob(doc, "cut.pdf");
+ const blob = await exportToBlob(doc);
return new Uint8Array(await blob.arrayBuffer());
}
diff --git a/frontend/src/editor/core/tools/pdfTextEditor/__tests__/textTagging.test.ts b/frontend/src/editor/core/tools/pdfTextEditor/__tests__/textTagging.test.ts
index 1a29ca0ac8..37720e7b6f 100644
--- a/frontend/src/editor/core/tools/pdfTextEditor/__tests__/textTagging.test.ts
+++ b/frontend/src/editor/core/tools/pdfTextEditor/__tests__/textTagging.test.ts
@@ -78,7 +78,7 @@ function shownTag(doc: EditorDocument, fragment: string) {
}
async function savedBytes(doc: EditorDocument): Promise<Uint8Array> {
- const { blob } = await exportToBlob(doc, "sample.pdf");
+ const blob = await exportToBlob(doc);
return new Uint8Array(await blob.arrayBuffer());
}
diff --git a/frontend/src/editor/core/tools/pdfTextEditor/hooks/useEditorOcr.test.tsx b/frontend/src/editor/core/tools/pdfTextEditor/hooks/useEditorOcr.test.tsx
index c278a2b3a4..ba134ab98f 100644
--- a/frontend/src/editor/core/tools/pdfTextEditor/hooks/useEditorOcr.test.tsx
+++ b/frontend/src/editor/core/tools/pdfTextEditor/hooks/useEditorOcr.test.tsx
@@ -122,10 +122,9 @@ describe("editor inline OCR", () => {
mocks.post.mockResolvedValue({
data: new Blob(["%PDF-1.7\nOCR result"], { type: "application/pdf" }),
});
- mocks.exportPdf.mockResolvedValue({
- blob: new Blob(["%PDF-1.7\ncurrent edits"], { type: "application/pdf" }),
- filename: "scan_edited.pdf",
- });
+ mocks.exportPdf.mockResolvedValue(
+ new Blob(["%PDF-1.7\ncurrent edits"], { type: "application/pdf" }),
+ );
mocks.checkCredits.mockResolvedValue(null);
mocks.ensureReady.mockResolvedValue(true);
mocks.assertAllowed.mockImplementation(() => {});
@@ -164,7 +163,7 @@ describe("editor inline OCR", () => {
it("blocks upload if protection is discovered during export", async () => {
mocks.exportPdf.mockImplementationOnce(async () => {
protectSource();
- return { blob: new Blob(["%PDF-1.7"]), filename: "scan.pdf" };
+ return new Blob(["%PDF-1.7"]);
});
const { result, store, onComplete } = await setup();
await act(() => result.current.runOcr());
@@ -224,7 +223,7 @@ describe("editor inline OCR", () => {
expect(new TextDecoder().decode(await readBytes(uploaded))).toContain(
"current edits",
);
- expect(mocks.exportPdf).toHaveBeenCalledWith(store.document, "scan.pdf");
+ expect(mocks.exportPdf).toHaveBeenCalledWith(store.document);
const output = onComplete.mock.calls[0][0];
expect(output.name).toBe("scan.pdf");
expect(new TextDecoder().decode(await readBytes(output))).toContain(
@@ -360,7 +359,7 @@ describe("editor inline OCR", () => {
mocks.assertAllowed.mockImplementation(() => {
throw new Error("File blocked");
});
- return { blob: new Blob(["%PDF-1.7"]), filename: "scan.pdf" };
+ return new Blob(["%PDF-1.7"]);
});
const { result, store } = await setup();
await act(() => result.current.runOcr());
diff --git a/frontend/src/editor/core/tools/pdfTextEditor/hooks/useEditorOcr.ts b/frontend/src/editor/core/tools/pdfTextEditor/hooks/useEditorOcr.ts
index 40bcb2b0a8..b0e8fef5a8 100644
--- a/frontend/src/editor/core/tools/pdfTextEditor/hooks/useEditorOcr.ts
+++ b/frontend/src/editor/core/tools/pdfTextEditor/hooks/useEditorOcr.ts
@@ -130,9 +130,9 @@ export function useEditorOcr({
"No OCR languages are installed on the server.",
),
);
- const { blob, filename } = await exportToBlob(doc, fileName);
+ const blob = await exportToBlob(doc);
if (!isCurrent()) return;
- const input = new File([blob], fileName ?? filename, {
+ const input = new File([blob], fileName ?? "edited.pdf", {
type: "application/pdf",
});
assertFilesNotBlocked([fileId ?? undefined]);
diff --git a/frontend/src/editor/core/tools/pdfTextEditor/util/exportPdf.ts b/frontend/src/editor/core/tools/pdfTextEditor/util/exportPdf.ts
index feb70264c4..6c47dde1ee 100644
--- a/frontend/src/editor/core/tools/pdfTextEditor/util/exportPdf.ts
+++ b/frontend/src/editor/core/tools/pdfTextEditor/util/exportPdf.ts
@@ -23,14 +23,8 @@ import {
assertSavedPdf,
} from "@editor/tools/pdfTextEditor/util/savedBytes";
-/** Serialize the editor document to a Blob plus the download filename. */
-export async function exportToBlob(
- doc: EditorDocument,
- sourceName?: string | null,
-): Promise<{
- blob: Blob;
- filename: string;
-}> {
+/** Serialize the editor document to a Blob. */
+export async function exportToBlob(doc: EditorDocument): Promise<Blob> {
const structureEdits = doc.structureEdits();
const annotationEdits = doc.annotationEdits();
const documentLanguage = doc.documentLanguageEdit();
@@ -49,10 +43,7 @@ export async function exportToBlob(
documentLanguage === undefined &&
!doc.markedTaggedChanged()
) {
- return {
- blob: pdfBlob(doc.openedBytes),
- filename: exportName(sourceName),
- };
+ return pdfBlob(doc.openedBytes);
}
bytes = doc.openedBytes;
} else {
@@ -117,7 +108,7 @@ export async function exportToBlob(
assertSavedPdf(bytes);
if (incremental) assertIncrementalAppend(bytes, doc.openedBytes);
- return { blob: pdfBlob(bytes), filename: exportName(sourceName) };
+ return pdfBlob(bytes);
}
function pagesWritingArtifactKinds(doc: EditorDocument): number[] {
@@ -196,13 +187,6 @@ function pdfBlob(bytes: Uint8Array): Blob {
});
}
-// Derive from the opened file's name so downloads don't all collide on
-// a generic "edited.pdf".
-function exportName(sourceName?: string | null): string {
- const base = (sourceName ?? "").replace(/\.pdf$/i, "").trim();
- return base ? `${base}_edited.pdf` : "edited.pdf";
-}
-
/**
* True when no page's content stream has been regenerated and none is waiting
* to be. `regenerated` is sticky, so this stays false for every later save in