Skip to content

Instantly share code, notes, and snippets.

@jdnichollsc
Created January 17, 2016 20:41
Show Gist options
  • Save jdnichollsc/5dae3ac221622fe826f1 to your computer and use it in GitHub Desktop.
Save jdnichollsc/5dae3ac221622fe826f1 to your computer and use it in GitHub Desktop.
Generate PDF in base64 with jsPDF
services.factory('PDF', ['$q', function ($q) {
var createSubtopic = function (doc, subtopic, top, pageWidth) {
doc.autoTable([
{ title: "Title", dataKey: "title" },
{ title: "Value", dataKey: "value" }
], [
{ title: "Estándar", value: subtopic.standard },
{ title: "Tema", value: subtopic.topic },
{ title: "Subtema", value: subtopic.subtopic }
], {
drawHeaderRow: function () {
return false;
},
columnStyles: {
title: { fillColor: [41, 128, 185], textColor: 255, fontStyle: 'bold' }
},
startY: top,
pageBreak: 'avoid'
});
var skills = ["Generales:", subtopic.generalSkills, "Específicas:", subtopic.specificSkills];
var totalRows = skills.length > subtopic.performances.length ? skills.length : subtopic.performances.length;
var rows = [];
for (var i = 0; i < totalRows; i++) {
var performance = subtopic.performances[i];
rows.push([
performance ? "- " + performance : "",
skills[i] || ""
]);
}
doc.autoTable(
["Desempeños", "Competencias"],
rows,
{
pageBreak: 'avoid',
styles: {
columnWidth: (pageWidth / 2),
overflow: 'linebreak'
},
startY: doc.autoTableEndPosY(),
drawCell: function (cell, data) {
if (data.row.index % 2 == 0 && data.column.dataKey == 1) {
doc.setFontStyle('bold');
doc.setFontSize(13);
//doc.rect(cell.x, cell.y, data.table.width / 2, cell.height, 'S');
doc.autoTableText(data.row.raw[1], data.settings.margin.left + data.table.width - data.table.width / 4, cell.y + cell.height / 2, {
halign: 'center',
valign: 'middle'
});
return false;
}
}
}
);
};
return {
generatePDF: function (date, area, degree, group, time, myClassNumber, classNumber, subtopics, totalTime, phases, note, environment) {
var deferred = $q.defer();
var sizeA4 = {
width: 595.28,
height: 841.89
};
var doc = new jsPDF('p', 'pt');
/*doc.setProperties({
title: 'Title',
subject: 'This is the subject',
author: 'James Hall',
keywords: 'generated, javascript, web 2.0, ajax',
creator: 'MEEE'
});*/
var pageWidth = doc.internal.pageSize.width - 80;
doc.autoTable(
["Fecha", "Área", "Grado", "Salón"],
[
[date, area, degree, group]
],
{
styles: {
columnWidth: (pageWidth / 4)
}
}
);
doc.autoTable(
["Hora", "Mi Clase", "Clase Santillana"],
[
[moment(time).format("h:mm a"), myClassNumber, classNumber]
],
{
styles: {
columnWidth: (pageWidth / 3)
},
startY: doc.autoTableEndPosY()
}
);
doc.setFontSize(26);
doc.text(40, doc.autoTableEndPosY() + 40, "Subtemas");
createSubtopic(doc, subtopics[0], doc.autoTableEndPosY() + 60, pageWidth);
for (var i = 1; i < subtopics.length; i++) {
var subtopic = subtopics[i];
createSubtopic(doc, subtopic, doc.autoTableEndPosY() + 20, pageWidth);
}
var lastPosY = doc.autoTableEndPosY();
doc.setFontSize(18);
doc.text(40, lastPosY += 40, "Tiempo de Clase: " + totalTime + " minutos");
doc.setFontSize(26);
doc.text(40, lastPosY += 50, "Actividades");
for (var i = 0; i < phases.length; i++) {
var phase = phases[i];
lastPosY += 40;
doc.setFontSize(18);
doc.text(40, lastPosY, phase.name);
doc.autoTable(
[{ title: "Recurso", dataKey: "name" },
{ title: "Actividad", dataKey: "description" },
{ title: "Tiempo (minutos)", dataKey: "time" }],
phase.activities,
{
styles: {
columnWidth: (pageWidth / 3),
overflow: 'linebreak'
},
startY: lastPosY + 10
}
);
lastPosY = doc.autoTableEndPosY();
}
doc.autoTable(
["Apuntes de la clase", "Ambiente de aprendizaje"],
[
[note, ""]
],
{
styles: {
columnWidth: (pageWidth / 2)
},
bodyStyles:{
rowHeight: 200,
overflow: 'linebreak'
},
startY: lastPosY + 40
}
);
convertToDataURLviaCanvas(environment.Image, function(base64Img){
// Base64DataURL
doc.addImage(base64Img, 'PNG', (pageWidth / 2)+60, doc.autoTableEndPosY() - 190, 220, 170);
deferred.resolve(doc.output('datauristring'));
}, 'image/png');
return deferred.promise;
}
};
} ]);
@GibranPolonsky
Copy link

Tanks this is great...
doc.output('datauristring')

@jdnichollsc
Copy link
Author

@CusProjects other example with NativeScript! 👍 https://play.nativescript.org/?template=play-ng&id=OGrw9s&v=8

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment