storytest.js 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. var snark =
  2. "<!DOCTYPE html>"
  3. +"<style>"
  4. +"#a { margin: 30px; }"
  5. +"#b { margin: 20px; }"
  6. +"#c { margin: 5px; }"
  7. +"#a { border: 1px solid red; }"
  8. +"#b { border: 1px solid green; }"
  9. +"#c { border: 1px solid blue; }"
  10. +"</style>"
  11. +"<body>"
  12. +"<div id=\"a\">"
  13. +"A"
  14. +"</div>"
  15. +"<div id=\"b\">"
  16. +"<div id=\"c\">"
  17. +"C"
  18. +"</div>"
  19. +"</div>"
  20. +"<p>\"Just the place for a Snark!\" the Bellman cried,<br>"
  21. +"As he landed his crew with care;<br>"
  22. +"Supporting each man on the top of the tide<br>"
  23. +"By a finger entwined in his hair.</p>"
  24. +"<P>Just the place for a Snark! I have said it twice:<br>"
  25. +"That alone should encourage the crew.<br>"
  26. +"Just the place for a Snark! I have said it thrice:<br>"
  27. +"What I tell you three times is true.</p>"
  28. +"<p>The crew was complete: it included a Boots-<br>"
  29. +"A maker of Bonnets and Hoods-<br>"
  30. +"A Barrister, brought to arrange their disputes-<br>"
  31. +"And a Broker, to value their goods.</p>"
  32. +"<p>A Billiard-marker, whose skill was immense,<br>"
  33. +"Might perhaps have won more than his share-<br>"
  34. +"But a Banker, engaged at enormous expense,<br>"
  35. +"Had the whole of their cash in his care.</p>"
  36. +"<p>There was also a Beaver, that paced on the deck,<br>"
  37. +"Or would sit making lace in the bow:<br>"
  38. +"And had often (the Bellman said) saved them from wreck,<br>"
  39. +"Though none of the sailors knew how.</p>"
  40. +"<p>There was one who was famed for the number of things<br>"
  41. +"He forgot when he entered the ship:<br>"
  42. +"His umbrella, his watch, all his jewels and rings,<br>"
  43. +"And the clothes he had bought for the trip.</p>"
  44. +"<div id=\"a\">"
  45. +"<p>He had forty-two boxes, all carefully packed,<br>"
  46. +"With his name painted clearly on each:<br>"
  47. +"But, since he omitted to mention the fact,<br>"
  48. +"They were all left behind on the beach.</p>"
  49. +"</div>"
  50. +"<p>The loss of his clothes hardly mattered, because<br>"
  51. +"He had seven coats on when he came,<br>"
  52. +"With three pair of boots-but the worst of it was,<br>"
  53. +"He had wholly forgotten his name.</p>"
  54. +"<p>He would answer to \"Hi!\" or to any loud cry,<br>"
  55. +"Such as \"Fry me!\" or \"Fritter my wig!\"<br>"
  56. +"To \"What-you-may-call-um!\" or \"What-was-his-name!\"<br>"
  57. +"But especially \"Thing-um-a-jig!\"</p>"
  58. +"<p>While, for those who preferred a more forcible word,<br>"
  59. +"He had different names from these:<br>"
  60. +"His intimate friends called him \"Candle-ends,\"<br>"
  61. +"And his enemies \"Toasted-cheese.\"</p>"
  62. +"<p>\"His form is ungainly-his intellect small-\"<br>"
  63. +"(So the Bellman would often remark)<br>"
  64. +"\"But his courage is perfect! And that, after all,<br>"
  65. +"Is the thing that one needs with a Snark.\"</p>"
  66. +"<p>He would joke with hyenas, returning their stare<br>"
  67. +"With an impudent wag of the head:<br>"
  68. +"And he once went a walk, paw-in-paw, with a bear,<br>"
  69. +"\"Just to keep up its spirits,\" he said.</p>"
  70. +"<p>He came as a Baker: but owned, when too late-<br>"
  71. +"And it drove the poor Bellman half-mad-<br>"
  72. +"He could only bake Bride-cake-for which, I may state,<br>"
  73. +"No materials were to be had.</p>"
  74. +"<p>The last of the crew needs especial remark,<br>"
  75. +"Though he looked an incredible dunce:<br>"
  76. +"He had just one idea-but, that one being \"Snark,\"<br>"
  77. +"The good Bellman engaged him at once.</p>"
  78. +"<p>He came as a Butcher: but gravely declared,<br>"
  79. +"When the ship had been sailing a week,<br>"
  80. +"He could only kill Beavers. The Bellman looked scared,<br>"
  81. +"And was almost too frightened to speak:</p>"
  82. +"<p>But at length he explained, in a tremulous tone,<br>"
  83. +"There was only one Beaver on board;<br>"
  84. +"And that was a tame one he had of his own,<br>"
  85. +"Whose death would be deeply deplored.</p>"
  86. +"<div id=\"b\">"
  87. +"<p>The Beaver, who happened to hear the remark,<br>"
  88. +"Protested, with tears in its eyes,<br>"
  89. +"That not even the rapture of hunting the Snark<br>"
  90. +"Could atone for that dismal surprise!</p>"
  91. +"</div>"
  92. +"<p style=\"-mupdf-leading:7pt;\">It strongly advised that the Butcher should be<br>"
  93. +"Conveyed in a separate ship:<br>"
  94. +"But the Bellman declared that would never agree<br>"
  95. +"With the plans he had made for the trip:</p>";
  96. snark +="<p style=\"-mupdf-leading:11pt;\">Navigation was always a difficult art,<br>"
  97. +"Though with only one ship and one bell:<br>"
  98. +"And he feared he must really decline, for his part,<br>"
  99. +"Undertaking another as well.</p>"
  100. +"<p style=\"-mupdf-leading:15pt;\">The Beaver's best course was, no doubt, to procure<br>"
  101. +"A second-hand dagger-proof coat-<br>"
  102. +"So the Baker advised it-and next, to insure<br>"
  103. +"Its life in some Office of note:</p>"
  104. +"<p style=\"-mupdf-leading:20pt;\">This the Banker suggested, and offered for hire<br>"
  105. +"(On moderate terms), or for sale,<br>"
  106. +"Two excellent Policies, one Against Fire,<br>"
  107. +"And one Against Damage From Hail.</p>"
  108. +"<p style=\"-mupdf-leading:30pt;\">Yet still, ever after that sorrowful day,<br>"
  109. +"Whenever the Butcher was by,<br>"
  110. +"The Beaver kept looking the opposite way,<br>"
  111. +"And appeared unaccountably shy.</p>"
  112. ;
  113. var films =
  114. [
  115. {
  116. "title":"Pulp Fiction",
  117. "director":"Quentin Tarantino",
  118. "year":"1994",
  119. "cast":[
  120. "John Travolta",
  121. "Samuel L Jackson",
  122. "Uma Thurman",
  123. "Bruce Willis",
  124. "Ving Rhames",
  125. "Harvey Keitel",
  126. "Tim Roth",
  127. "Bridget Fonda"
  128. ]
  129. },
  130. {
  131. "title":"The Usual Suspects",
  132. "director":"Bryan Singer",
  133. "year":"1995",
  134. "cast":[
  135. "Kevin Spacey",
  136. "Gabriel Byrne",
  137. "Chazz Palminteri",
  138. "Benicio Del Toro",
  139. "Kevin Pollak",
  140. "Pete Postlethwaite",
  141. "Steven Baldwin"
  142. ]
  143. },
  144. {
  145. "title":"Fight Club",
  146. "director":"David Fincher",
  147. "year":"1999",
  148. "cast":[
  149. "Brad Pitt",
  150. "Edward Norton",
  151. "Helena Bonham Carter"
  152. ]
  153. }
  154. ];
  155. var festival_template =
  156. "<html><head><title>Why do we have a title? Why not?</title></head>"
  157. +"<body><h1 style=\"text-align:center\">Hook Norton Film Festival</h1>"
  158. +"<ol>"
  159. +"<li id=\"filmtemplate\">"
  160. +"<b id=\"filmtitle\"></b>"
  161. +"<dl>"
  162. +"<dt>Director<dd id=\"director\">"
  163. +"<dt>Release Year<dd id=\"filmyear\">"
  164. +"<dt>Cast<dd id=\"cast\">"
  165. +"</dl>"
  166. +"</li>"
  167. +"<ul>"
  168. +"</body></html>";
  169. var mediabox = [0, 0, 512, 640];
  170. var margin = 10;
  171. /* First one made with precooked content. */
  172. {
  173. var writer = new DocumentWriter("out.pdf", "PDF", "");
  174. var buf = new Buffer(snark.length);
  175. buf.write(snark);
  176. var story = new Story(buf, "", 11);
  177. var placed;
  178. do
  179. {
  180. var where = [
  181. mediabox[0] + margin,
  182. mediabox[1] + margin,
  183. mediabox[2] - margin,
  184. mediabox[3] - margin
  185. ];
  186. var dev = writer.beginPage(mediabox);
  187. placed = story.place(where);
  188. story.draw(dev, Matrix.identity);
  189. writer.endPage();
  190. }
  191. while (placed.more);
  192. writer.close();
  193. }
  194. /* Now one made with programmatic content. */
  195. {
  196. var writer = new DocumentWriter("out2.pdf", "PDF", "");
  197. var story = new Story(null, "", 11);
  198. var dom = story.document();
  199. var body = dom.body();
  200. body.appendChild(dom.createTextNode("This is some text."));
  201. var tmp = dom.createElement("b");
  202. body.appendChild(tmp);
  203. tmp.appendChild(dom.createTextNode("This is some bold text."));
  204. body.appendChild(dom.createTextNode("This is some normal text."));
  205. var placed;
  206. do
  207. {
  208. var where = [
  209. mediabox[0] + margin,
  210. mediabox[1] + margin,
  211. mediabox[2] - margin,
  212. mediabox[3] - margin
  213. ];
  214. var dev = writer.beginPage(mediabox);
  215. placed = story.place(where);
  216. story.draw(dev, Matrix.identity);
  217. writer.endPage(writer);
  218. }
  219. while (placed.more);
  220. writer.close();
  221. }
  222. /* Now a combination of the two. */
  223. {
  224. var writer = new DocumentWriter("out3.pdf", "PDF", "");
  225. var buf = new Buffer(festival_template.length);
  226. buf.write(festival_template);
  227. var story = new Story(buf, "", 11);
  228. var dom = story.document();
  229. var body = dom.body();
  230. var templat = body.find(null, "id", "filmtemplate");
  231. var placed;
  232. for (var f = 0; f < films.length; f++)
  233. {
  234. var film = templat.clone();
  235. /* Now fill in some of the template. */
  236. var tmp = film.find(null, "id", "filmtitle");
  237. tmp.appendChild(dom.createTextNode(films[f].title));
  238. tmp = film.find(null, "id", "director");
  239. tmp.appendChild(dom.createTextNode(films[f].director));
  240. tmp = film.find(null, "id", "filmyear");
  241. tmp.appendChild(dom.createTextNode(films[f].year));
  242. tmp = film.find(null, "id", "cast");
  243. for (var j = 0; j < films[f].cast.length; j++)
  244. {
  245. tmp.appendChild(dom.createTextNode(films[f].cast[j]));
  246. tmp.appendChild(dom.createElement("br"));
  247. }
  248. templat.parent().appendChild(film);
  249. }
  250. /* Remove the template. */
  251. templat.remove();
  252. do
  253. {
  254. var where = [
  255. mediabox[0] + margin,
  256. mediabox[1] + margin,
  257. mediabox[2] - margin,
  258. mediabox[3] - margin
  259. ];
  260. var dev = writer.beginPage(mediabox);
  261. placed = story.place(where);
  262. story.draw(dev, Matrix.identity);
  263. writer.endPage();
  264. }
  265. while (placed.more);
  266. writer.close();
  267. }