crfPrint.js 4.6 KB

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