123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155 |
- //printing section
- function checkBlob(){
- print("checkBlob: "+config.blob);
- if (config.blob) {
- clearInterval(config.blobInterval);
- config.a.href = config.window.URL.createObjectURL(config.blob);
- print("HREF: "+config.a.href);
- config.a.download = 'test.pdf';
- config.a.click();
- config.window.URL.revokeObjectURL(config.a.href);
- }
- config.count=config.count+1;
- print("Eval: "+config.count);
- if (config.count>100){
- clearInterval(config.blobInterval);
- }
- }
- function printForm(){
- config.doc=new PDFDocument();
- //config.doc.end();
- let stream = config.doc.pipe(blobStream()).on("finish",function(){
- config.blob=stream.toBlob("application/pdf");});
-
- print("BLob: "+config.blob);
- config.a = config.document.createElement("a");
- config.document.body.appendChild(config.a);
- config.a.innerHTML="Download PDF";
- config.a.style = "display: none";
- config.count=0;
- //run until blob is set
- config.blobInterval=setInterval(checkBlob,1000);
- //pick data from crfForm list
- print("Printing form");
- printHeader();
- setData(formatPrintData);
- }
- function printHeader(){
- config.doc.fontSize(25).text(config.formConfig.form['formName']);
- config.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){
- print('Printing for '+f);
- let e=val[f];
- let entry=new Object();
- entry[f]=e.o[e.f];
- printPDF(entry,
- {name:f,caption:e.t,type:'string'},null);
- }
- config.doc.moveDown();
- }
- function formatPrintData(){
- qS=config.formConfig.dataQueries;
- for (let q in qS){
- print('Setting up '+q);
- let qData=qS[q];
- print('Number of rows: '+qData.rows.length);
- if (qData.rows.length>0){
- config.doc.fontSize(20).text(qData.title);
- }
- for (let i=0;i<qData.rows.length;i++){
- let entry=qData.rows[i];
- for (let f in qData.fields){
- let field=qData.fields[f];
- let lookup=null;
- if (field.lookup){
- lookup=config.formConfig.lookup[field.lookup.queryName];
- }
- if (field.hidden) continue;
- printPDF(entry,field,lookup);
- }
- }
- config.doc.moveDown();
- }
- print("All done");
- config.doc.end();
- }
- function printPDF(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;
-
- print('printPDF: entry['+field.name+']='+entry[field.name]);
- let v=entry[field.name];
- if (lookup!=null){
- v=lookup.LUT[v];
- }
- 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};
- config.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
- config.doc.font('Courier').text(label,opt);
-
- //align last row of description w/ first row of value
- config.doc.moveUp();
- //store x value for later use
- let tx=config.doc.x;
- let ty=config.doc.y;
- //shift for value output
- config.doc.x+=w1+spacing;
-
- config.doc.font('Courier-Bold').text(v,opt);
- //restore x value
- config.doc.x=tx;
-
- }
|