mirror of
https://github.com/mozilla/pdf.js.git
synced 2025-04-20 15:18:08 +02:00
Merge pull request #16588 from calixteman/editor_stamp_2
[Editor] Add support for printing/saving newly added Stamp annotations
This commit is contained in:
commit
88c7c8b5bf
9 changed files with 519 additions and 19 deletions
|
@ -490,8 +490,24 @@ class Driver {
|
|||
});
|
||||
let promise = loadingTask.promise;
|
||||
|
||||
if (task.annotationStorage) {
|
||||
for (const annotation of Object.values(task.annotationStorage)) {
|
||||
if (annotation.bitmapName) {
|
||||
promise = promise.then(async doc => {
|
||||
const response = await fetch(
|
||||
new URL(`./images/${annotation.bitmapName}`, window.location)
|
||||
);
|
||||
const blob = await response.blob();
|
||||
annotation.bitmap = await createImageBitmap(blob);
|
||||
|
||||
return doc;
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (task.save) {
|
||||
promise = loadingTask.promise.then(async doc => {
|
||||
promise = promise.then(async doc => {
|
||||
if (!task.annotationStorage) {
|
||||
throw new Error("Missing `annotationStorage` entry.");
|
||||
}
|
||||
|
|
BIN
test/images/firefox_logo.png
Executable file
BIN
test/images/firefox_logo.png
Executable file
Binary file not shown.
After Width: | Height: | Size: 169 KiB |
|
@ -7814,5 +7814,155 @@
|
|||
"rounds": 1,
|
||||
"type": "eq",
|
||||
"annotations": true
|
||||
},
|
||||
{
|
||||
"id": "tracemonkey-stamp-editor-print",
|
||||
"file": "pdfs/tracemonkey.pdf",
|
||||
"md5": "9a192d8b1a7dc652a19835f6f08098bd",
|
||||
"rounds": 1,
|
||||
"lastPage": 1,
|
||||
"type": "eq",
|
||||
"print": true,
|
||||
"annotationStorage": {
|
||||
"pdfjs_internal_editor_0": {
|
||||
"annotationType": 13,
|
||||
"pageIndex": 0,
|
||||
"bitmapName": "firefox_logo.png",
|
||||
"bitmapId": "image_c29eaeb9-8839-4a09-bf7c-75a5785805cd_0",
|
||||
"rect": [37.5, 32.406246185302734, 496.5000057220459, 519.1875],
|
||||
"rotation": 0
|
||||
},
|
||||
"pdfjs_internal_editor_1": {
|
||||
"annotationType": 13,
|
||||
"bitmapId": "image_c29eaeb9-8839-4a09-bf7c-75a5785805cd_0",
|
||||
"pageIndex": 0,
|
||||
"rect": [
|
||||
458.06250572204584,
|
||||
473.04686737060547,
|
||||
571.5000057220459,
|
||||
582.7187461853027
|
||||
],
|
||||
"rotation": 0
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": "tracemonkey-stamp-editor-save-print",
|
||||
"file": "pdfs/tracemonkey.pdf",
|
||||
"md5": "9a192d8b1a7dc652a19835f6f08098bd",
|
||||
"rounds": 1,
|
||||
"lastPage": 1,
|
||||
"type": "eq",
|
||||
"save": true,
|
||||
"print": true,
|
||||
"annotationStorage": {
|
||||
"pdfjs_internal_editor_0": {
|
||||
"annotationType": 13,
|
||||
"pageIndex": 0,
|
||||
"bitmapName": "firefox_logo.png",
|
||||
"bitmapId": "image_c29eaeb9-8839-4a09-bf7c-75a5785805cd_0",
|
||||
"rect": [37.5, 32.406246185302734, 496.5000057220459, 519.1875],
|
||||
"rotation": 0
|
||||
},
|
||||
"pdfjs_internal_editor_1": {
|
||||
"annotationType": 13,
|
||||
"bitmapId": "image_c29eaeb9-8839-4a09-bf7c-75a5785805cd_0",
|
||||
"pageIndex": 0,
|
||||
"rect": [
|
||||
458.06250572204584,
|
||||
473.04686737060547,
|
||||
571.5000057220459,
|
||||
582.7187461853027
|
||||
],
|
||||
"rotation": 0
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": "tracemonkey-stamp-editor-print-2-pages",
|
||||
"file": "pdfs/tracemonkey.pdf",
|
||||
"md5": "9a192d8b1a7dc652a19835f6f08098bd",
|
||||
"rounds": 1,
|
||||
"lastPage": 2,
|
||||
"type": "eq",
|
||||
"print": true,
|
||||
"annotationStorage": {
|
||||
"pdfjs_internal_editor_0": {
|
||||
"annotationType": 13,
|
||||
"pageIndex": 0,
|
||||
"bitmapName": "firefox_logo.png",
|
||||
"bitmapId": "image_c29eaeb9-8839-4a09-bf7c-75a5785805cd_0",
|
||||
"rect": [37.5, 32.406246185302734, 496.5000057220459, 519.1875],
|
||||
"rotation": 0
|
||||
},
|
||||
"pdfjs_internal_editor_1": {
|
||||
"annotationType": 13,
|
||||
"bitmapId": "image_c29eaeb9-8839-4a09-bf7c-75a5785805cd_0",
|
||||
"pageIndex": 0,
|
||||
"rect": [
|
||||
458.06250572204584,
|
||||
473.04686737060547,
|
||||
571.5000057220459,
|
||||
582.7187461853027
|
||||
],
|
||||
"rotation": 0
|
||||
},
|
||||
"pdfjs_internal_editor_2": {
|
||||
"annotationType": 13,
|
||||
"bitmapId": "image_c29eaeb9-8839-4a09-bf7c-75a5785805cd_0",
|
||||
"pageIndex": 1,
|
||||
"rect": [
|
||||
458.06250572204584,
|
||||
473.04686737060547,
|
||||
571.5000057220459,
|
||||
582.7187461853027
|
||||
],
|
||||
"rotation": 0
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": "tracemonkey-stamp-editor-save-print-2-pages",
|
||||
"file": "pdfs/tracemonkey.pdf",
|
||||
"md5": "9a192d8b1a7dc652a19835f6f08098bd",
|
||||
"rounds": 1,
|
||||
"lastPage": 2,
|
||||
"type": "eq",
|
||||
"save": true,
|
||||
"print": true,
|
||||
"annotationStorage": {
|
||||
"pdfjs_internal_editor_0": {
|
||||
"annotationType": 13,
|
||||
"pageIndex": 0,
|
||||
"bitmapName": "firefox_logo.png",
|
||||
"bitmapId": "image_c29eaeb9-8839-4a09-bf7c-75a5785805cd_0",
|
||||
"rect": [37.5, 32.406246185302734, 496.5000057220459, 519.1875],
|
||||
"rotation": 0
|
||||
},
|
||||
"pdfjs_internal_editor_1": {
|
||||
"annotationType": 13,
|
||||
"bitmapId": "image_c29eaeb9-8839-4a09-bf7c-75a5785805cd_0",
|
||||
"pageIndex": 0,
|
||||
"rect": [
|
||||
458.06250572204584,
|
||||
473.04686737060547,
|
||||
571.5000057220459,
|
||||
582.7187461853027
|
||||
],
|
||||
"rotation": 0
|
||||
},
|
||||
"pdfjs_internal_editor_2": {
|
||||
"annotationType": 13,
|
||||
"bitmapId": "image_c29eaeb9-8839-4a09-bf7c-75a5785805cd_0",
|
||||
"pageIndex": 1,
|
||||
"rect": [
|
||||
458.06250572204584,
|
||||
473.04686737060547,
|
||||
571.5000057220459,
|
||||
582.7187461853027
|
||||
],
|
||||
"rotation": 0
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
|
|
|
@ -2134,6 +2134,58 @@ describe("api", function () {
|
|||
await loadingTask.destroy();
|
||||
});
|
||||
|
||||
it("write a new stamp annotation, save the pdf and check that the same image has the same ref", async function () {
|
||||
if (isNodeJS) {
|
||||
pending("Cannot create a bitmap from Node.js.");
|
||||
}
|
||||
|
||||
const TEST_IMAGES_PATH = "../images/";
|
||||
const filename = "firefox_logo.png";
|
||||
const path = new URL(TEST_IMAGES_PATH + filename, window.location).href;
|
||||
|
||||
const response = await fetch(path);
|
||||
const blob = await response.blob();
|
||||
const bitmap = await createImageBitmap(blob);
|
||||
|
||||
let loadingTask = getDocument(buildGetDocumentParams("empty.pdf"));
|
||||
let pdfDoc = await loadingTask.promise;
|
||||
pdfDoc.annotationStorage.setValue("pdfjs_internal_editor_0", {
|
||||
annotationType: AnnotationEditorType.STAMP,
|
||||
rect: [12, 34, 56, 78],
|
||||
rotation: 0,
|
||||
bitmap,
|
||||
bitmapId: "im1",
|
||||
pageIndex: 0,
|
||||
});
|
||||
pdfDoc.annotationStorage.setValue("pdfjs_internal_editor_1", {
|
||||
annotationType: AnnotationEditorType.STAMP,
|
||||
rect: [112, 134, 156, 178],
|
||||
rotation: 0,
|
||||
bitmapId: "im1",
|
||||
pageIndex: 0,
|
||||
});
|
||||
|
||||
const data = await pdfDoc.saveDocument();
|
||||
await loadingTask.destroy();
|
||||
|
||||
loadingTask = getDocument(data);
|
||||
pdfDoc = await loadingTask.promise;
|
||||
const page = await pdfDoc.getPage(1);
|
||||
const opList = await page.getOperatorList();
|
||||
|
||||
// The pdf contains two stamp annotations with the same image.
|
||||
// The image should be stored only once in the pdf and referenced twice.
|
||||
// So we can verify that the image is referenced twice in the opList.
|
||||
|
||||
for (let i = 0; i < opList.fnArray.length; i++) {
|
||||
if (opList.fnArray[i] === OPS.paintImageXObject) {
|
||||
expect(opList.argsArray[i][0]).toEqual("img_p0_1");
|
||||
}
|
||||
}
|
||||
|
||||
await loadingTask.destroy();
|
||||
});
|
||||
|
||||
describe("Cross-origin", function () {
|
||||
let loadingTask;
|
||||
function _checkCanLoad(expectSuccess, filename, options) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue