尝试使用来自JSON数据的pdfMake动态创建pdf,除了我的图像外,报告运行正常。
我已尝试将命名键用于对象以及推入数组,甚至直接进入docDefinition,看起来它们包含console.log上的数据,但总是返回undefined。
如果我直接将它分配到each
(//imageData["image0"] = testDataURL;
)之外,它实际上唯一有效的时间但是这对我不起作用,因为我需要得到每个“学生”记录的每个图像的dataurl 。
它必须是范围问题或异步问题。请帮忙。
function getBase64Image(url, onSuccess, onFail, outputFormat){
var canvas = document.createElement('CANVAS'),
ctx = canvas.getContext('2d'),
img = new Image();
//img.crossOrigin = 'Anonymous';
img.onload = function(){
var dataURL = "";
canvas.height = this.height;
canvas.width = this.width;
ctx.drawImage(this, 0, 0);
dataURL = canvas.toDataURL(outputFormat);
onSuccess(dataURL);
canvas = null;
};
img.onerror = function(e){
onFail("No Image");
}
img.src = url;
} buildPdf = function(studentData){
var docDefinition = {"content": [],"images":{},"styles":{},"defaultStyle":{}};
var imageData = {};
var imageData2 = [];
$j(studentData).each(function(index, student){
docDefinition.content.push({"text": "Medical Care Plan","style": "title"});
docDefinition.content.push({"columns":[{"text": [{"text": "Name: ", "style": "label"},{"text": student.first_name + " " + student.last_name + "\n"},
{"text" : "Grade: ", "style": "label"},
{"text" : student.grade_level + "\t"},
{"text" : "HRM: ", "style": "label"},
{"text" : student.home_room + "\t"},
{"text" : "Bus#: ", "style": "label"},
{"text" : student.bus_route + "\n"},
{"text": "Teacher: ", "style": "label"},
{"text" : "Teacher Name goes here\n\n"},
{"text": "Doctor: ", "style": "label"},
{"text" : student.doctor_name + "\t"},
{"text" : "Doctors Phone: ", "style": "label"},
{"text" : student.doctor_phone + "\n"},
{"text" : "Health Card: ", "style": "label"},
{"text" : student.ON_HealthCard_Number}],width:400 },
{"image": "image0","fit" : [100, 100], "alignment" : "left", width : "*"}]});
getBase64Image('/admin/stp/'+student.stuId+'ph.jpeg',function(base64Img){
imageData["image0"]=base64Img;
imageData2[index]=base64Img;
//docDefinition.images["image"+index]=base64Img;
},function(errorMsg){
getBase64Image('/images/TLDSB_Custom/photo_not_available_BW.png',function(base64Img){
imageData["image0"]=base64Img;
imageData2[index]=base64Img;
});
});
});
//console.log(imageData2.length);
//imageData["image0"] = testDataURL;
//docDefinition.images["image0"]=imageData["image0"];
docDefinition.styles.title = { fontSize: 20, bold: true, alignment: 'center'};
docDefinition.styles.heading = { fontSize: 14,margin: [0, 5, 0, 0],bold: true};
docDefinition.styles.subheading = { fontSize: 14,margin: [5, 0, 0, 0],bold: true};
docDefinition.styles.smallHeader = { fontSize: 10,margin: [0, 5, 0, 0],bold:true};
docDefinition.styles.small = { fontSize: 8 };
docDefinition.styles.label = { bold: true };
docDefinition.styles.indent = { margin: [20, 0, 0, 0] };
docDefinition.styles.tableHeader = { bold: true, fontSize: 13 };
docDefinition.defaultStyle = { fontSize: 10 };
//console.log(studentData);
console.log(docDefinition);
//pdfMake.createPdf(docDefinition).open();
pdfMake.createPdf(docDefinition).download('MedicalCarePlan.pdf');
closeLoading();
};