123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182 |
- var crfPrint={};
- //printing section
- //
- crfPrint.set=
- function(parentClass){
- this.parent=parentClass;
- }
- crfPrint.checkBlob=
- function(){
- this.parent.print("checkBlob: "+this.blob);
- if (this.blob) {
- clearInterval(this.blobInterval);
- this.a.href = this.parent.config.window.URL.createObjectURL(this.blob);
- this.parent.print("HREF: "+this.a.href);
- this.a.download = 'test.pdf';
- this.a.click();
- this.parent.config.window.URL.revokeObjectURL(this.a.href);
- }
- this.count=this.count+1;
- this.parent.print("Eval: "+this.count);
- if (this.count>100){
- clearInterval(this.blobInterval);
- }
- }
- crfPrint.printForm=
- function(){
- let that=this;
- let action=function(){that.afterScripts();};
- LABKEY.Utils.requiresScript(["crfTecant/blob-stream.js","crfTecant/pdfkit.standalone.js"],action);
- }
- crfPrint.afterScripts=
- function(){
- let config=this.parent.config;
- this.doc=new PDFDocument();
- let that=this;
- //config.doc.end();
- let action=function(){that.blob=that.stream.toBlob("application/pdf");};
- this.stream = this.doc.pipe(blobStream()).on("finish",action);
-
- this.parent.print("BLob: "+this.blob);
- this.a = this.parent.config.document.createElement("a");
- this.parent.config.document.body.appendChild(this.a);
- this.a.innerHTML="Download PDF";
- this.a.style = "display: none";
- this.count=0;
- //run until blob is set
- let iAction=function(){that.checkBlob();}
- this.blobInterval=setInterval(iAction,1000);
- //pick data from crfForm list
- this.parent.print("Printing form");
- this.printHeader();
- let foo=function(){that.formatPrintData();};
- this.parent.setData(foo);
- }
- crfPrint.printHeader=
- function(){
- let config=this.parent.config;
- this.doc.fontSize(25).text(config.formConfig.form['formName']);
- this.doc.moveDown();
- let crfEntry=config.formConfig.crfEntry;
- let site=config.formConfig.currentSite;
- let val=new Object();
- let user=config.formConfig.user;
- val['A']={o:crfEntry,f:'EudraCTNumber',t:'Eudra CT Number'};
- val['B']={o:crfEntry,f:'StudyCoordinator',t:'Study Coordinator'};
- val['C']={o:crfEntry,f:'StudySponsor',t:'Study Sponsor'};
- val['D']={o:site,f:'siteName',t:'Site'};
- val['E']={o:site,f:'sitePhone',t:'Phone'};
- val['F']={o:user,f:'DisplayName',t:'Investigator'};
- for (let f in val){
- this.parent.print('Printing for '+f);
- let e=val[f];
- let entry=new Object();
- entry[f]=e.o[e.f];
- this.printPDF(entry,
- {name:f,caption:e.t,type:'string'},null);
- }
- this.doc.moveDown();
- }
- crfPrint.formatPrintData=
- function(){
- let config=this.parent.config;
- qS=this.parent.getQueryList();
- for (let q in qS){
- this.parent.print('Setting up '+q);
- let qData=this.parent.getQuerySnapshot(q);
- let qLayout=this.parent.getQueryLayout(q);
- this.parent.print('Number of rows: '+qData.rows.length);
- if (qData.rows.length>0){
- this.doc.fontSize(20).text(qLayout.title);
- }
- let fields=qLayout.fields;
- for (let i=0;i<qData.rows.length;i++){
- let entry=qData.rows[i];
- for (let f in fields){
- let field=fields[f];
- let lookup=null;
- if (field.lookup){
- lookup=this.parent.getLookup(field.lookup.queryName);
- }
- if (field.hidden) continue;
- this.printPDF(entry,field,lookup);
- }
- }
- this.doc.moveDown();
- }
- this.parent.print("All done");
- this.doc.end();
- }
- crfPrint.printPDF=
- function(entry,field,lookup){
- //object field should have a name, type, caption
- //entry should have field.name
- //lookup is null or has a lookup table LUT
- //for value v of entry[field.name]
- //
- //the total width of a A4 page is 598 px,
- //left margin is 72. With a right margin of 50,
- //the total available with is 476 px.
-
- let w=476;
- let spacing=25;
- let w1=(w-spacing)*0.5;
- let fontSize=14;
-
- this.parent.print('printPDF: entry['+field.name+']='+entry[field.name]);
- let v=entry[field.name];
- if (lookup!=null){
- v=lookup.LUT[v];
- }
- this.parent.print('printPDF: field type:'+field.type);
- if (field.type=="date"){
- let d=new Date(v);
- v=d.getDate()+'/'+(d.getMonth()+1)+'/'+d.getFullYear();
- }
- if (v===null) v=' / ';
- if (v===undefined) v=' / ';
- //measure text
- let label=field.caption;
- let opt={width:w1};
- this.doc.fontSize(fontSize);
-
- //for more eloquent display the height of the text
- //can be measured prior to output
- //use currentLineHeight to scale height
- //let lineH=config.doc.currentLineHeight(1);
- //let h=config.doc.heightOfString(label,opt)/lineH;
- //print label
- this.doc.font('Courier').text(label,opt);
-
- //align last row of description w/ first row of value
- this.doc.moveUp();
- //store x value for later use
- let tx=this.doc.x;
- let ty=this.doc.y;
- //shift for value output
- this.doc.x+=w1+spacing;
-
- this.doc.font('Courier-Bold').text(v,opt);
- //restore x value
- this.doc.x=tx;
-
- }
|