writer_spec.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /* Copyright 2020 Mozilla Foundation
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. import { Dict, Name, Ref } from "../../src/core/primitives.js";
  16. import { incrementalUpdate, writeDict } from "../../src/core/writer.js";
  17. import { bytesToString } from "../../src/shared/util.js";
  18. import { StringStream } from "../../src/core/stream.js";
  19. describe("Writer", function () {
  20. describe("Incremental update", function () {
  21. it("should update a file with new objects", function () {
  22. const originalData = new Uint8Array();
  23. const newRefs = [
  24. { ref: Ref.get(123, 0x2d), data: "abc\n" },
  25. { ref: Ref.get(456, 0x4e), data: "defg\n" },
  26. ];
  27. const xrefInfo = {
  28. newRef: Ref.get(789, 0),
  29. startXRef: 314,
  30. fileIds: ["id", ""],
  31. rootRef: null,
  32. infoRef: null,
  33. encryptRef: null,
  34. filename: "foo.pdf",
  35. info: {},
  36. };
  37. let data = incrementalUpdate({ originalData, xrefInfo, newRefs });
  38. data = bytesToString(data);
  39. const expected =
  40. "\nabc\n" +
  41. "defg\n" +
  42. "789 0 obj\n" +
  43. "<< /Size 790 /Prev 314 /Type /XRef /Index [0 1 123 1 456 1 789 1] " +
  44. "/ID [(id) (\x01#Eg\x89\xab\xcd\xef\xfe\xdc\xba\x98vT2\x10)] " +
  45. "/W [1 1 2] /Length 16>> stream\n" +
  46. "\x00\x01\xff\xff" +
  47. "\x01\x01\x00\x2d" +
  48. "\x01\x05\x00\x4e" +
  49. "\x01\x0a\x00\x00\n" +
  50. "endstream\n" +
  51. "endobj\n" +
  52. "startxref\n" +
  53. "10\n" +
  54. "%%EOF\n";
  55. expect(data).toEqual(expected);
  56. });
  57. it("should update a file, missing the /ID-entry, with new objects", function () {
  58. const originalData = new Uint8Array();
  59. const newRefs = [{ ref: Ref.get(123, 0x2d), data: "abc\n" }];
  60. const xrefInfo = {
  61. newRef: Ref.get(789, 0),
  62. startXRef: 314,
  63. fileIds: null,
  64. rootRef: null,
  65. infoRef: null,
  66. encryptRef: null,
  67. filename: "foo.pdf",
  68. info: {},
  69. };
  70. let data = incrementalUpdate({ originalData, xrefInfo, newRefs });
  71. data = bytesToString(data);
  72. const expected =
  73. "\nabc\n" +
  74. "789 0 obj\n" +
  75. "<< /Size 790 /Prev 314 /Type /XRef /Index [0 1 123 1 789 1] " +
  76. "/W [1 1 2] /Length 12>> stream\n" +
  77. "\x00\x01\xff\xff" +
  78. "\x01\x01\x00\x2d" +
  79. "\x01\x05\x00\x00\n" +
  80. "endstream\n" +
  81. "endobj\n" +
  82. "startxref\n" +
  83. "5\n" +
  84. "%%EOF\n";
  85. expect(data).toEqual(expected);
  86. });
  87. });
  88. describe("writeDict", function () {
  89. it("should write a Dict", function () {
  90. const dict = new Dict(null);
  91. dict.set("A", Name.get("B"));
  92. dict.set("B", Ref.get(123, 456));
  93. dict.set("C", 789);
  94. dict.set("D", "hello world");
  95. dict.set("E", "(hello\\world)");
  96. dict.set("F", [1.23001, 4.50001, 6]);
  97. const gdict = new Dict(null);
  98. gdict.set("H", 123.00001);
  99. const string = "a stream";
  100. const stream = new StringStream(string);
  101. stream.dict = new Dict(null);
  102. stream.dict.set("Length", string.length);
  103. gdict.set("I", stream);
  104. dict.set("G", gdict);
  105. dict.set("J", true);
  106. dict.set("K", false);
  107. dict.set("NullArr", [null, 10]);
  108. dict.set("NullVal", null);
  109. const buffer = [];
  110. writeDict(dict, buffer, null);
  111. const expected =
  112. "<< /A /B /B 123 456 R /C 789 /D (hello world) " +
  113. "/E (\\(hello\\\\world\\)) /F [1.23 4.5 6] " +
  114. "/G << /H 123 /I << /Length 8>> stream\n" +
  115. "a stream\n" +
  116. "endstream>> /J true /K false " +
  117. "/NullArr [null 10] /NullVal null>>";
  118. expect(buffer.join("")).toEqual(expected);
  119. });
  120. it("should write a Dict in escaping PDF names", function () {
  121. const dict = new Dict(null);
  122. dict.set("\xfeA#", Name.get("hello"));
  123. dict.set("B", Name.get("#hello"));
  124. dict.set("C", Name.get("he\xfello\xff"));
  125. const buffer = [];
  126. writeDict(dict, buffer, null);
  127. const expected = "<< /#feA#23 /hello /B /#23hello /C /he#fello#ff>>";
  128. expect(buffer.join("")).toEqual(expected);
  129. });
  130. });
  131. describe("XFA", function () {
  132. it("should update AcroForm when no datasets in XFA array", function () {
  133. const originalData = new Uint8Array();
  134. const newRefs = [];
  135. const acroForm = new Dict(null);
  136. acroForm.set("XFA", [
  137. "preamble",
  138. Ref.get(123, 0),
  139. "postamble",
  140. Ref.get(456, 0),
  141. ]);
  142. const acroFormRef = Ref.get(789, 0);
  143. const xfaDatasetsRef = Ref.get(101112, 0);
  144. const xfaData = "<hello>world</hello>";
  145. const xrefInfo = {
  146. newRef: Ref.get(131415, 0),
  147. startXRef: 314,
  148. fileIds: null,
  149. rootRef: null,
  150. infoRef: null,
  151. encryptRef: null,
  152. filename: "foo.pdf",
  153. info: {},
  154. };
  155. let data = incrementalUpdate({
  156. originalData,
  157. xrefInfo,
  158. newRefs,
  159. hasXfa: true,
  160. xfaDatasetsRef,
  161. hasXfaDatasetsEntry: false,
  162. acroFormRef,
  163. acroForm,
  164. xfaData,
  165. xref: {},
  166. });
  167. data = bytesToString(data);
  168. const expected =
  169. "\n" +
  170. "789 0 obj\n" +
  171. "<< /XFA [(preamble) 123 0 R (datasets) 101112 0 R (postamble) 456 0 R]>>\n" +
  172. "endobj\n" +
  173. "101112 0 obj\n" +
  174. "<< /Type /EmbeddedFile /Length 20>>\n" +
  175. "stream\n" +
  176. "<hello>world</hello>\n" +
  177. "endstream\n" +
  178. "endobj\n" +
  179. "131415 0 obj\n" +
  180. "<< /Size 131416 /Prev 314 /Type /XRef /Index [0 1 789 1 101112 1 131415 1] /W [1 1 2] /Length 16>> stream\n" +
  181. "\u0000\u0001ÿÿ\u0001\u0001\u0000\u0000\u0001[\u0000\u0000\u0001¹\u0000\u0000\n" +
  182. "endstream\n" +
  183. "endobj\n" +
  184. "startxref\n" +
  185. "185\n" +
  186. "%%EOF\n";
  187. expect(data).toEqual(expected);
  188. });
  189. });
  190. });