No Description

createPDF.js 2.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. var fonts = {
  2. Roboto: {
  3. normal: 'fonts/Roboto-Regular.ttf',
  4. bold: 'fonts/Roboto-Medium.ttf',
  5. italics: 'fonts/Roboto-Italic.ttf',
  6. bolditalics: 'fonts/Roboto-.ttf',
  7. }
  8. };
  9. var pdfMake = require('pdfmake/build/pdfmake.js');
  10. var pdfFonts = require('pdfmake/build/vfs_fonts.js');
  11. pdfMake.vfs = pdfFonts.pdfMake.vfs;
  12. function table(course) {
  13. return {
  14. widths: [50, 100, 50, 50],
  15. headerRows: 2,
  16. body: [
  17. [{text: `${course.code} ${course.title}`, bold: true, colSpan: 4, alignment: 'center'}, {}, {}, {}],
  18. ['Seccion', 'Profesor', 'Lugar', 'Horas'],
  19. ].concat( course.sections.map( function(section) {
  20. var res = [section.code];
  21. if (section.hasOwnProperty('professors') && !section.professors.length) {
  22. res = res.concat( section.professors.reduce( function (acc, professor) {
  23. return acc + '\n' + professor.first_name + ' ' + professor.last_name;
  24. }));
  25. } else {
  26. res = res.concat('');
  27. }
  28. if (section.hasOwnProperty('schedules') && !section.professors.length) {
  29. res = res.concat( section.schedules.reduce( function (acc, schedule) {
  30. return acc + '\n' + schedule.building + ' ' + schedule.room;
  31. }));
  32. res = res.concat( section.schedules.reduce( function (acc, schedule) {
  33. return acc + '\n' + schedule.time_start + '-' + schedule.time_end;
  34. }));
  35. } else {
  36. res = res.concat(['', '']);
  37. }
  38. return res;
  39. }))
  40. }
  41. }
  42. var docDef = {
  43. content: [
  44. {text: `Horario ${data.dept.name} ${data.alpha}`, style: 'header'},
  45. {text: 'sujeto a cambios', style: 'header', fontSize: 18},
  46. ].concat( data.courses.map( function(course) {
  47. return {
  48. style: 'tableStyle',
  49. alignment: 'center',
  50. table: table(course)
  51. }
  52. })),
  53. styles: {
  54. header: {
  55. fontSize: 22,
  56. bold: true,
  57. alignment: 'center',
  58. },
  59. tableStyle: {
  60. margin: [50,10,0,15]
  61. },
  62. }
  63. };
  64. console.log(docDef);
  65. pdfMake.createPdf(docDef).download();
  66. // window.location.replace("/dashboard");