basic.lpr 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. program basic;
  2. {$MODE OBJFPC}
  3. {$MODESWITCH EXTERNALCLASS}
  4. uses
  5. Web,
  6. jsPDF;
  7. var
  8. doc: TjsPDF;
  9. opt: string;
  10. edSelExample: TJSHTMLSelectElement;
  11. btDownload: TJSHTMLButtonElement;
  12. ebPreview: TJSHTMLEmbedElement;
  13. procedure edSelExampleChange;
  14. begin
  15. doc := TjsPDF.new;
  16. opt := edSelExample.value;
  17. case opt of
  18. 'hello': doc.text('Hello world. I love TjsPDF! :-)', 10, 10);
  19. 'font':
  20. begin
  21. doc.text('This is the default font.', 20, 20);
  22. doc.setFont('courier');
  23. doc.setFontStyle('normal');
  24. doc.text('This is courier normal.', 20, 30);
  25. doc.setFont('times');
  26. doc.setFontStyle('italic');
  27. doc.text('This is times italic.', 20, 40);
  28. doc.setFont('helvetica');
  29. doc.setFontStyle('bold');
  30. doc.text('This is helvetica bold.', 20, 50);
  31. doc.setFont('courier');
  32. doc.setFontStyle('bolditalic');
  33. doc.text('This is courier bolditalic.', 20, 60);
  34. doc.setFont('times');
  35. doc.setFontStyle('normal');
  36. doc.text('This is centred text.', 105, 80, nil, nil, 'center');
  37. doc.text('And a little bit more underneath it.', 105, 90, nil, nil, 'center');
  38. doc.text('This is right aligned text', 200, 100, nil, nil, 'right');
  39. doc.text('And some more', 200, 110, nil, nil, 'right');
  40. doc.text('Back to left',20, 120);
  41. doc.text('10 degrees rotated', 20, 140, nil, 10);
  42. doc.text('-10 degrees rotated', 20, 160, nil, -10);
  43. end;
  44. 'two_page':
  45. begin
  46. doc.text('Hello world!', 20, 20);
  47. doc.text('This is client-side Javascript, pumping out a PDF.', 20, 30);
  48. doc.addPage('l', 'a6');
  49. doc.text('Do you like that?', 20, 20);
  50. end;
  51. 'circles':
  52. begin
  53. doc.ellipse(40, 20, 10, 5);
  54. doc.setFillColor(0,0,255);
  55. doc.ellipse(80, 20, 10, 5, 'F');
  56. doc.setLineWidth(1);
  57. doc.setDrawColor(0);
  58. doc.setFillColor(255,0,0);
  59. doc.circle(120, 20, 5, 'FD');
  60. end;
  61. 'lines':
  62. begin
  63. doc.line(20, 20, 60, 20); // horizontal line
  64. doc.setLineWidth(0.5);
  65. doc.line(20, 25, 60, 25);
  66. doc.setLineWidth(1);
  67. doc.line(20, 30, 60, 30);
  68. doc.setLineWidth(1.5);
  69. doc.line(20, 35, 60, 35);
  70. doc.setDrawColor(255,0,0); // draw red lines
  71. doc.setLineWidth(0.1);
  72. doc.line(100, 20, 100, 60); // vertical line
  73. doc.setLineWidth(0.5);
  74. doc.line(105, 20, 105, 60);
  75. doc.setLineWidth(1);
  76. doc.line(110, 20, 110, 60);
  77. doc.setLineWidth(1.5);
  78. doc.line(115, 20, 115, 60);
  79. end;
  80. 'rectangles':
  81. begin
  82. // Empty square
  83. doc.rect(20, 20, 10, 10);
  84. // Filled square
  85. doc.rect(40, 20, 10, 10, 'F');
  86. // Empty red square
  87. doc.setDrawColor(255,0,0);
  88. doc.rect(60, 20, 10, 10);
  89. // Filled square with red borders
  90. doc.setDrawColor(255,0,0);
  91. doc.rect(80, 20, 10, 10, 'FD');
  92. // Filled red square
  93. doc.setDrawColor(0);
  94. doc.setFillColor(255,0,0);
  95. doc.rect(100, 20, 10, 10, 'F');
  96. // Filled red square with black borders
  97. doc.setDrawColor(0);
  98. doc.setFillColor(255,0,0);
  99. doc.rect(120, 20, 10, 10, 'FD');
  100. // Black square with rounded corners
  101. doc.setDrawColor(0);
  102. doc.setFillColor(255, 255, 255);
  103. doc.roundedRect(140, 20, 10, 10, 3, 3, 'FD');
  104. end;
  105. 'triangles':
  106. begin
  107. doc.triangle(60, 100, 60, 120, 80, 110, 'FD');
  108. doc.setLineWidth(1);
  109. doc.setDrawColor(255,0,0);
  110. doc.setFillColor(0,0,255);
  111. doc.triangle(100, 100, 110, 100, 120, 130, 'FD');
  112. end;
  113. end;
  114. ebPreview.src := doc.output_('datauristring');
  115. btDownload.disabled := opt = '';
  116. end;
  117. procedure btDownloadClick;
  118. begin
  119. doc.save(opt + '.pdf');
  120. end;
  121. begin
  122. edSelExample := TJSHTMLSelectElement(document.getElementById('edSelExample'));
  123. edSelExample.addEventListener('change', @edSelExampleChange);
  124. btDownload := TJSHTMLButtonElement(document.getElementById('btDownload'));
  125. btDownload.addEventListener('click', @btDownloadClick);
  126. ebPreview := TJSHTMLEmbedElement(document.getElementById('ebPreview'));
  127. end.