jspdf.pas 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  1. {
  2. This file is part of the Pas2JS run time library.
  3. Copyright (C) 2018 Silvio Clecio (silvioprog)
  4. Pascal mapping for jsPDF: https://parall.ax/products/jspdf
  5. See the file COPYING.FPC, included in this distribution,
  6. for details about the copyright.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. **********************************************************************}
  11. unit jsPDF;
  12. {$MODE OBJFPC}
  13. {$MODESWITCH EXTERNALCLASS}
  14. interface
  15. uses
  16. Types,
  17. JS,
  18. SysUtils;
  19. type
  20. TjsPDF = class external name 'jsPDF'
  21. public class var
  22. { jsPDF.API is a STATIC property of jsPDF class. jsPDF.API is an object you
  23. can add methods and properties to. The methods / properties you add will
  24. show up in new jsPDF objects. One property is prepopulated. It is the
  25. 'events' Object. Plugin authors can add topics, callbacks to this object.
  26. These will be reassigned to all new instances of jsPDF. }
  27. API: TJSObject external name 'API';
  28. { The version of jsPDF. }
  29. version: string external name 'version';
  30. public
  31. { Creates new jsPDF document object instance. If the first parameter
  32. (orientation) is an object, it will be interpreted as an object of named
  33. parameters. }
  34. constructor new(
  35. { Measurement unit to be used when coordinates are specified.
  36. Possible values are "pt" (points), "mm" (Default), "cm", "in" or "px". }
  37. orientation: JSValue = 'p';
  38. { Measurement unit to be used when coordinates are specified. Possible
  39. values are "pt" (points), "mm" (Default), "cm", "in" or "px". }
  40. const &unit: string = 'mm';
  41. { The format of the first page. Can be:
  42. a0 - a10
  43. b0 - b10
  44. c0 - c10
  45. dl
  46. letter
  47. government-letter
  48. legal
  49. junior-legal
  50. ledger
  51. tabloid
  52. credit-card
  53. Default is "a4". If you want to use your own format just pass instead
  54. of one of the above predefined formats the size as an number-array,
  55. e.g. [595.28, 841.89] }
  56. const format: string = 'a4'); overload;
  57. constructor new(const orientation: string = 'p'; const &unit: string = 'mm';
  58. const format: string = 'a4'); overload;
  59. { Add a custom font to the current instance. }
  60. procedure addFont(
  61. { PDF specification full name for the font. }
  62. const postScriptName: string;
  63. { PDF-document-instance-specific label assinged to the font. }
  64. const id: string = '';
  65. { Style of the Font. }
  66. const fontStyle: string = '';
  67. { Encoding_name-to-Font_metrics_object mapping. }
  68. encoding: JSValue = nil);
  69. { Adds (and transfers the focus to) new page to the PDF document. }
  70. function addPage(
  71. { The format of the new page. Can be:
  72. a0 - a10
  73. b0 - b10
  74. c0 - c10
  75. dl
  76. letter
  77. government-letter
  78. legal
  79. junior-legal
  80. ledger
  81. tabloid
  82. credit-card
  83. Default is "a4". If you want to use your own format just pass instead
  84. of one of the above predefined formats the size as an number-array,
  85. e.g. [595.28, 841.89] }
  86. const format: string = 'a4';
  87. { Orientation of the new page. Possible values are "portrait" or
  88. "landscape" (or shortcuts "p" (Default), "l"). }
  89. const orientation: string = 'p'): TjsPDF; overload;
  90. function addPage(format: array of Double = nil;
  91. const orientation: string = 'p'): TjsPDF; overload;
  92. { Adds an circle to PDF. }
  93. function circle(
  94. { Coordinate (in units declared at inception of PDF document) against left
  95. edge of the page. }
  96. x: JSValue = Undefined;
  97. { Coordinate (in units declared at inception of PDF document) against
  98. upper edge of the page. }
  99. y: JSValue = Undefined;
  100. { Radius (in units declared at inception of PDF document). }
  101. r: JSValue = Undefined;
  102. { A string specifying the painting style or null. Valid styles
  103. include: 'S' [default] - stroke, 'F' - fill, and 'DF' (or 'FD') - fill
  104. then stroke. A null value postpones setting the style so that a shape
  105. may be composed using multiple method calls. The last drawing method
  106. call used to define the shape should not have a null style argument. }
  107. const style: string = 'S'): TjsPDF;
  108. { All .clip() after calling drawing ops with a style argument of null. }
  109. function clip(const rule: string): TjsPDF;
  110. { Deletes a page from the PDF. }
  111. function deletePage: TjsPDF;
  112. { Adds an ellipse to PDF. }
  113. function ellipse(
  114. { Coordinate (in units declared at inception of PDF document) against left
  115. edge of the page. }
  116. x: JSValue = Undefined;
  117. { Coordinate (in units declared at inception of PDF document) against
  118. upper edge of the page. }
  119. y: JSValue = Undefined;
  120. { Radius along x axis (in units declared at inception of PDF document). }
  121. rx: JSValue = Undefined;
  122. { Radius along y axis (in units declared at inception of PDF document). }
  123. ry: JSValue = Undefined;
  124. { A string specifying the painting style or null. Valid styles
  125. include: 'S' [default] - stroke, 'F' - fill, and 'DF' (or 'FD') - fill
  126. then stroke. A null value postpones setting the style so that a shape
  127. may be composed using multiple method calls. The last drawing method
  128. call used to define the shape should not have a null style argument. }
  129. const style: string = 'S'): TjsPDF;
  130. { Get global value of CharSpace. }
  131. function getCharSpace: Double;
  132. { }
  133. function getCreationDate: TJSDate;
  134. { Gets the stroke color for upcoming elements. }
  135. function getDrawColor: string;
  136. { GUID. }
  137. function getFileId: string;
  138. { Gets the fill color for upcoming elements. }
  139. function getFillColor: string;
  140. { Returns an object - a tree of fontName to fontStyle relationships
  141. available to active PDF document. }
  142. function getFontList: TJSObject;
  143. { Gets the fontsize for upcoming text elements. }
  144. function getFontSize: Double;
  145. { Gets the LineHeightFactor, default: 1.15. }
  146. function getLineHeightFactor: Double;
  147. { Get value of R2L functionality. }
  148. function getR2L: Boolean;
  149. { Gets the text color for upcoming elements. }
  150. function getTextColor: string;
  151. { }
  152. function insertPage(beforePage: TJSObject): TjsPDF;
  153. { Draw a line on the current page. }
  154. function line(x1: JSValue = Undefined; y1: JSValue = Undefined;
  155. x2: JSValue = Undefined; y2: JSValue = Undefined): TjsPDF;
  156. { Adds series of curves (straight lines or cubic bezier curves) to canvas,
  157. starting at `x`, `y` coordinates. All data points in `lines` are relative
  158. to last line origin. `x`, `y` become x1,y1 for first line / curve in the
  159. set. For lines you only need to specify [x2, y2] - (ending point) vector
  160. against x1, y1 starting point. For bezier curves you need to specify
  161. [x2,y2,x3,y3,x4,y4] - vectors to control points 1, 2, ending point. All
  162. vectors are against the start of the curve - x1,y1. }
  163. function lines(
  164. { Array of *vector* shifts as pairs (lines) or sextets (cubic bezier
  165. curves). }
  166. lines: array of TDoubleDynArray;
  167. { Coordinate (in units declared at inception of PDF document) against left
  168. edge of the page. }
  169. x: JSValue = Undefined;
  170. { Coordinate (in units declared at inception of PDF document) against
  171. upper edge of the page. }
  172. y: JSValue = Undefined;
  173. { (Defaults to [1.0,1.0]) x,y Scaling factor for all vectors. Elements can
  174. be any floating number Sub-one makes drawing smaller. Over-one grows the
  175. drawing. Negative flips the direction. }
  176. scale: TDoubleDynArray = [1.0, 1.0];
  177. { A string specifying the painting style or null. Valid styles
  178. include: 'S' [default] - stroke, 'F' - fill, and 'DF' (or 'FD') - fill
  179. then stroke. A null value postpones setting the style so that a shape
  180. may be composed using multiple method calls. The last drawing method
  181. call used to define the shape should not have a null style argument. }
  182. const style: string = 'S';
  183. { If true, the path is closed with a straight line from the end of the
  184. last curve to the starting point. }
  185. closed: Boolean = False): TjsPDF;
  186. { }
  187. function movePage(targetPage: TJSObject; beforePage: TJSObject): TjsPDF;
  188. { Generates the PDF document. If `type` argument is undefined, output is raw
  189. body of resulting PDF returned as a string. }
  190. function output(
  191. { A string identifying one of the possible output types. Possible values
  192. are 'arraybuffer', 'blob', 'bloburi'/'bloburl',
  193. 'datauristring'/'dataurlstring', 'datauri'/'dataurl',
  194. 'dataurlnewwindow'. }
  195. const &type: string;
  196. { An object providing some additional signalling to PDF generator.
  197. Possible options are 'filename'. }
  198. options: TJSObject = nil): TjsPDF;
  199. function output_(const &type: string;
  200. options: TJSObject = nil): string; external name 'output';
  201. { Adds a rectangle to PDF. }
  202. function rect(
  203. { Coordinate (in units declared at inception of PDF document) against left
  204. edge of the page. }
  205. x: JSValue = Undefined;
  206. { Coordinate (in units declared at inception of PDF document) against
  207. upper edge of the page. }
  208. y: JSValue = Undefined;
  209. { Width (in units declared at inception of PDF document). }
  210. w: JSValue = Undefined;
  211. { Height (in units declared at inception of PDF document). }
  212. h: JSValue = Undefined;
  213. { A string specifying the painting style or null. Valid styles
  214. include: 'S' [default] - stroke, 'F' - fill, and 'DF' (or 'FD') - fill
  215. then stroke. A null value postpones setting the style so that a shape
  216. may be composed using multiple method calls. The last drawing method
  217. call used to define the shape should not have a null style argument. }
  218. const style: string = 'S'): TjsPDF;
  219. { Adds a rectangle with rounded corners to PDF. }
  220. function roundedRect(
  221. { Coordinate (in units declared at inception of PDF document) against left
  222. edge of the page. }
  223. x: JSValue = Undefined;
  224. { Coordinate (in units declared at inception of PDF document) against
  225. upper edge of the page. }
  226. y: JSValue = Undefined;
  227. { Width (in units declared at inception of PDF document). }
  228. w: JSValue = Undefined;
  229. { Height (in units declared at inception of PDF document). }
  230. h: JSValue = Undefined;
  231. { Radius along x axis (in units declared at inception of PDF document). }
  232. rx: JSValue = Undefined;
  233. { Radius along y axis (in units declared at inception of PDF document). }
  234. ry: JSValue = Undefined;
  235. { A string specifying the painting style or null. Valid styles
  236. include: 'S' [default] - stroke, 'F' - fill, and 'DF' (or 'FD') - fill
  237. then stroke. A null value postpones setting the style so that a shape
  238. may be composed using multiple method calls. The last drawing method
  239. call used to define the shape should not have a null style argument. }
  240. const style: string = 'S'): TjsPDF;
  241. { Saves as PDF document. An alias of jsPDF.output('save', 'filename.pdf').
  242. Uses FileSaver.js-method saveAs. }
  243. function save(
  244. { The filename including extension. }
  245. const filename: TFileName;
  246. { An Object with additional options, possible options: 'returnPromise'. }
  247. options: TJSObject = nil): TjsPDF;
  248. { Set global value of CharSpace. }
  249. function setCharSpace(charSpace: JSValue): TjsPDF;
  250. { }
  251. function setCreationDate(date: TJSDate): TjsPDF;
  252. { Set the display mode options of the page like zoom and layout. }
  253. function setDisplayMode(
  254. { You can pass an integer or percentage as a string. 2 will scale the
  255. document up 2x, '200%' will scale up by the same amount. You can also
  256. set it to 'fullwidth', 'fullheight', 'fullpage', or 'original'. Only
  257. certain PDF readers support this, such as Adobe Acrobat. }
  258. zoom: JSValue;
  259. { Layout mode can be:
  260. 'continuous' - this is the default continuous scroll.
  261. 'single' - the single page mode only shows one page at a time.
  262. 'twoleft' - two column left mode, first page starts on the left, and
  263. 'tworight' - pages are laid out in two columns, with the first page on
  264. the right. This would be used for books. }
  265. const layout: string = '';
  266. { 'UseOutlines' - it shows the outline of the document on the left.
  267. 'UseThumbs' - shows thumbnails along the left.
  268. 'FullScreen' - prompts the user to enter fullscreen mode. }
  269. const pmode: string = ''): TjsPDF;
  270. { Adds a properties to the PDF document. }
  271. function setDocumentProperties(
  272. { property_name-to-property_value object structure. }
  273. A: TJSObject): TjsPDF;
  274. { Sets the stroke color for upcoming elements. Depending on the number of
  275. arguments given, Gray, RGB, or CMYK color space is implied. When only ch1
  276. is given, "Gray" color space is implied and it must be a value in the
  277. range from 0.00 (solid black) to to 1.00 (white) if values are
  278. communicated as String types, or in range from 0 (black) to 255 (white) if
  279. communicated as Number type. The RGB-like 0-255 range is provided for
  280. backward compatibility. When only ch1,ch2,ch3 are given, "RGB" color space
  281. is implied and each value must be in the range from 0.00 (minimum intensity)
  282. to to 1.00 (max intensity) if values are communicated as String types, or
  283. from 0 (min intensity) to to 255 (max intensity) if values are
  284. communicated as Number types. The RGB-like 0-255 range is provided for
  285. backward compatibility. When ch1,ch2,ch3,ch4 are given, "CMYK" color space
  286. is implied and each value must be a in the range from 0.00 (0% concentration)
  287. to to 1.00 (100% concentration) Because JavaScript treats fixed point
  288. numbers badly (rounds to floating point nearest to binary representation)
  289. it is highly advised to communicate the fractional numbers as String types,
  290. not JavaScript Number type. }
  291. function setDrawColor(
  292. (* Color channel value or {string} ch1 color value in hexadecimal,
  293. example: '#FFFFFF'. *)
  294. ch1: JSValue = Undefined;
  295. { Color channel value. }
  296. ch2: JSValue = Undefined;
  297. { Color channel value. }
  298. ch3: JSValue = Undefined;
  299. { Color channel value. }
  300. ch4: JSValue = Undefined): TjsPDF;
  301. { }
  302. function setFileId(
  303. { GUID. }
  304. const value: string): TjsPDF;
  305. { Sets the fill color for upcoming elements. Depending on the number of arguments given, Gray, RGB, or CMYK color space is implied. When only ch1 is given, "Gray" color space is implied and it must be a value in the range from 0.00 (solid black) to to 1.00 (white) if values are communicated as String types, or in range from 0 (black) to 255 (white) if communicated as Number type. The RGB-like 0-255 range is provided for backward compatibility. When only ch1,ch2,ch3 are given, "RGB" color space is implied and each value must be in the range from 0.00 (minimum intensity) to to 1.00 (max intensity) if values are communicated as String types, or from 0 (min intensity) to to 255 (max intensity) if values are communicated as Number types. The RGB-like 0-255 range is provided for backward compatibility. When ch1,ch2,ch3,ch4 are given, "CMYK" color space is implied and each value must be a in the range from 0.00 (0% concentration) to to 1.00 (100% concentration) Because JavaScript treats fixed point numbers badly (rounds to floating point nearest to binary representation) it is highly advised to communicate the fractional numbers as String types, not JavaScript Number type. }
  306. function setFillColor(
  307. (* Color channel value or {string} ch1 color value in hexadecimal,
  308. example: '#FFFFFF'. *)
  309. ch1: JSValue = Undefined;
  310. { Color channel value. }
  311. ch2: JSValue = Undefined;
  312. { Color channel value. }
  313. ch3: JSValue = Undefined;
  314. { Color channel value. }
  315. ch4: JSValue = Undefined): TjsPDF;
  316. { Sets text font face, variant for upcoming text elements. See output of
  317. jsPDF.getFontList() for possible font names, styles. }
  318. function setFont(
  319. { Font name or family. Example: "times". }
  320. const fontName: string;
  321. { Font style or variant. Example: "italic". }
  322. const fontStyle: string): TjsPDF; overload;
  323. function setFont(const fontName: string): TjsPDF; overload;
  324. { Sets font size for upcoming text elements. }
  325. function setFontSize(
  326. { Font size in points. }
  327. size: JSValue): TjsPDF;
  328. { Switches font style or variant for upcoming text elements, while keeping
  329. the font face or family same. See output of jsPDF.getFontList() for
  330. possible font names, styles. }
  331. function setFontStyle(
  332. { Font style or variant. Example: "italic". }
  333. const style: string): TjsPDF;
  334. (* Sets the line cap styles. See {jsPDF.CapJoinStyles} for variants. *)
  335. function setLineCap(
  336. { A string or number identifying the type of line cap. }
  337. style: JSValue): TjsPDF;
  338. { Sets the LineHeightFactor of proportion. }
  339. function setLineHeightFactor(
  340. { LineHeightFactor value. Default: 1.15. }
  341. value: JSValue): TjsPDF;
  342. (* Sets the line join styles. See {jsPDF.CapJoinStyles} for variants. *)
  343. function setLineJoin(
  344. { A string or number identifying the type of line join. }
  345. style: JSValue): TjsPDF;
  346. { Sets line width for upcoming lines. }
  347. function setLineWidth(
  348. { Line width (in units declared at inception of PDF document). }
  349. width: JSValue): TjsPDF;
  350. { Adds (and transfers the focus to) new page to the PDF document. }
  351. function setPage(
  352. { Switch the active page to the page number specified. }
  353. page: Cardinal): TjsPDF;
  354. { Set value of R2L functionality. }
  355. function setR2L(value: Boolean = True): TjsPDF;
  356. { Sets the text color for upcoming elements. Depending on the number of
  357. arguments given, Gray, RGB, or CMYK color space is implied. When only ch1
  358. is given, "Gray" color space is implied and it must be a value in the
  359. range from 0.00 (solid black) to to 1.00 (white) if values are
  360. communicated as String types, or in range from 0 (black) to 255 (white) if
  361. communicated as Number type. The RGB-like 0-255 range is provided for
  362. backward compatibility. When only ch1,ch2,ch3 are given, "RGB" color space
  363. is implied and each value must be in the range from 0.00 (minimum intensity)
  364. to to 1.00 (max intensity) if values are communicated as String types, or
  365. from 0 (min intensity) to to 255 (max intensity) if values are communicated
  366. as Number types. The RGB-like 0-255 range is provided for backward
  367. compatibility. When ch1,ch2,ch3,ch4 are given, "CMYK" color space is
  368. implied and each value must be a in the range from 0.00 (0% concentration)
  369. to to 1.00 (100% concentration) Because JavaScript treats fixed point
  370. numbers badly (rounds to floating point nearest to binary representation)
  371. it is highly advised to communicate the fractional numbers as String types,
  372. not JavaScript Number type. }
  373. function setTextColor(
  374. (* Color channel value or {string} ch1 color value in hexadecimal,
  375. example: '#FFFFFF'. *)
  376. ch1: JSValue = Undefined;
  377. { Color channel value. }
  378. ch2: JSValue = Undefined;
  379. { Color channel value. }
  380. ch3: JSValue = Undefined;
  381. { Color channel value. }
  382. ch4: JSValue = Undefined): TjsPDF;
  383. { Adds text to page. Supports adding multiline text when 'text' argument is
  384. an Array of Strings. }
  385. function text(
  386. { String or array of strings to be added to the page. Each line is shifted
  387. one line down per font, spacing settings declared before this call. }
  388. const text: string;
  389. { Coordinate (in units declared at inception of PDF document) against left
  390. edge of the page. }
  391. x: JSValue = Undefined;
  392. { Coordinate (in units declared at inception of PDF document) against
  393. upper edge of the page. }
  394. y: JSValue = Undefined;
  395. { Collection of settings signaling how the text must be encoded. }
  396. options: JSValue = Undefined): TjsPDF; overload; varargs;
  397. function text(const text: array of string;
  398. x: JSValue = Undefined; y: JSValue = Undefined;
  399. options: JSValue = nil): TjsPDF; overload; varargs;
  400. { Adds a triangle to PDF. }
  401. function triangle(
  402. { Coordinate (in units declared at inception of PDF document) against left
  403. edge of the page. }
  404. x1: JSValue = Undefined;
  405. { Coordinate (in units declared at inception of PDF document) against
  406. upper edge of the page. }
  407. y1: JSValue = Undefined;
  408. { Coordinate (in units declared at inception of PDF document) against
  409. left edge of the page. }
  410. x2: JSValue = Undefined;
  411. { Coordinate (in units declared at inception of PDF document) against
  412. upper edge of the page. }
  413. y2: JSValue = Undefined;
  414. { Coordinate (in units declared at inception of PDF document) against
  415. left edge of the page. }
  416. x3: JSValue = Undefined;
  417. { Coordinate (in units declared at inception of PDF document) against
  418. upper edge of the page. }
  419. y3: JSValue = Undefined;
  420. { A string specifying the painting style or null. Valid styles
  421. include: 'S' [default] - stroke, 'F' - fill, and 'DF' (or 'FD') - fill
  422. then stroke. A null value postpones setting the style so that a shape
  423. may be composed using multiple method calls. The last drawing method
  424. call used to define the shape should not have a null style argument. }
  425. const style: string = ''): TjsPDF;
  426. end;
  427. implementation
  428. end.