crfPrint.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. //printing section
  2. function checkBlob(){
  3. print("checkBlob: "+config.blob);
  4. if (config.blob) {
  5. clearInterval(config.blobInterval);
  6. config.a.href = config.window.URL.createObjectURL(config.blob);
  7. print("HREF: "+config.a.href);
  8. config.a.download = 'test.pdf';
  9. config.a.click();
  10. config.window.URL.revokeObjectURL(config.a.href);
  11. }
  12. config.count=config.count+1;
  13. print("Eval: "+config.count);
  14. if (config.count>100){
  15. clearInterval(config.blobInterval);
  16. }
  17. }
  18. function printForm(){
  19. config.doc=new PDFDocument();
  20. //config.doc.end();
  21. let stream = config.doc.pipe(blobStream()).on("finish",function(){
  22. config.blob=stream.toBlob("application/pdf");});
  23. print("BLob: "+config.blob);
  24. config.a = config.document.createElement("a");
  25. config.document.body.appendChild(config.a);
  26. config.a.innerHTML="Download PDF";
  27. config.a.style = "display: none";
  28. config.count=0;
  29. //run until blob is set
  30. config.blobInterval=setInterval(checkBlob,1000);
  31. //pick data from crfForm list
  32. print("Printing form");
  33. printHeader();
  34. setData(formatPrintData);
  35. }
  36. function printHeader(){
  37. config.doc.fontSize(25).text(config.formConfig.form['formName']);
  38. config.doc.moveDown();
  39. let crfEntry=config.formConfig.crfEntry;
  40. let site=config.formConfig.currentSite;
  41. let val=new Object();
  42. let user=config.formConfig.user;
  43. val['A']={o:crfEntry,f:'EudraCTNumber',t:'Eudra CT Number'};
  44. val['B']={o:crfEntry,f:'StudyCoordinator',t:'Study Coordinator'};
  45. val['C']={o:crfEntry,f:'StudySponsor',t:'Study Sponsor'};
  46. val['D']={o:site,f:'siteName',t:'Site'};
  47. val['E']={o:site,f:'sitePhone',t:'Phone'};
  48. val['F']={o:user,f:'DisplayName',t:'Investigator'};
  49. for (let f in val){
  50. print('Printing for '+f);
  51. let e=val[f];
  52. let entry=new Object();
  53. entry[f]=e.o[e.f];
  54. printPDF(entry,
  55. {name:f,caption:e.t,type:'string'},null);
  56. }
  57. config.doc.moveDown();
  58. }
  59. function formatPrintData(){
  60. qS=config.formConfig.dataQueries;
  61. for (let q in qS){
  62. print('Setting up '+q);
  63. let qData=qS[q];
  64. print('Number of rows: '+qData.rows.length);
  65. if (qData.rows.length>0){
  66. config.doc.fontSize(20).text(qData.title);
  67. }
  68. for (let i=0;i<qData.rows.length;i++){
  69. let entry=qData.rows[i];
  70. for (let f in qData.fields){
  71. let field=qData.fields[f];
  72. let lookup=null;
  73. if (field.lookup){
  74. lookup=config.formConfig.lookup[field.lookup.queryName];
  75. }
  76. if (field.hidden) continue;
  77. printPDF(entry,field,lookup);
  78. }
  79. }
  80. config.doc.moveDown();
  81. }
  82. print("All done");
  83. config.doc.end();
  84. }
  85. function printPDF(entry,field,lookup){
  86. //object field should have a name, type, caption
  87. //entry should have field.name
  88. //lookup is null or has a lookup table LUT
  89. //for value v of entry[field.name]
  90. //
  91. //the total width of a A4 page is 598 px,
  92. //left margin is 72. With a right margin of 50,
  93. //the total available with is 476 px.
  94. let w=476;
  95. let spacing=25;
  96. let w1=(w-spacing)*0.5;
  97. let fontSize=14;
  98. print('printPDF: entry['+field.name+']='+entry[field.name]);
  99. let v=entry[field.name];
  100. if (lookup!=null){
  101. v=lookup.LUT[v];
  102. }
  103. print('printPDF: field type:'+field.type);
  104. if (field.type=="date"){
  105. let d=new Date(v);
  106. v=d.getDate()+'/'+(d.getMonth()+1)+'/'+d.getFullYear();
  107. }
  108. if (v===null) v=' / ';
  109. if (v===undefined) v=' / ';
  110. //measure text
  111. let label=field.caption;
  112. let opt={width:w1};
  113. config.doc.fontSize(fontSize);
  114. //for more eloquent display the height of the text
  115. //can be measured prior to output
  116. //use currentLineHeight to scale height
  117. //let lineH=config.doc.currentLineHeight(1);
  118. //let h=config.doc.heightOfString(label,opt)/lineH;
  119. //print label
  120. config.doc.font('Courier').text(label,opt);
  121. //align last row of description w/ first row of value
  122. config.doc.moveUp();
  123. //store x value for later use
  124. let tx=config.doc.x;
  125. let ty=config.doc.y;
  126. //shift for value output
  127. config.doc.x+=w1+spacing;
  128. config.doc.font('Courier-Bold').text(v,opt);
  129. //restore x value
  130. config.doc.x=tx;
  131. }