Skip to content

Commit

Permalink
Add HIIT Function to display in PDF
Browse files Browse the repository at this point in the history
  • Loading branch information
StarOne01 committed May 29, 2024
1 parent 95c461c commit bf41fca
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 61 deletions.
6 changes: 3 additions & 3 deletions assets/js/firebase.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ const app = initializeApp(firebaseConfig);
const storage = getStorage(app);
function goPdf(e, mode) {
let [DataDef, name,PhNo] = printPdf(e,mode);
/* const storageRef = ref(storage, "ClientPdfs/"+ name); // Replace with your desired storage path
///*
const storageRef = ref(storage, "ClientPdfs/"+ name); // Replace with your desired storage path
pdfMake.createPdf(DataDef)
.getBlob((blob) => {
if (blob) {
Expand Down Expand Up @@ -55,11 +56,10 @@ pdfMake.createPdf(DataDef)
});
});
};
});*/
});//*/
}
// Initialize the blob and name variables
getPDFBtn.addEventListener("click",(e) => goPdf(e,0));
getExBtn.addEventListener("click",(e) => goPdf(e,1));
getBothBtn.addEventListener("click",(e) => goPdf(e,2));


121 changes: 63 additions & 58 deletions assets/js/printPdf.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,41 +6,7 @@

function fetchAndConvert(item) {
// URL of the image you want to fetch
console.log(item)
var imageUrl = "https://starone01.github.io/foodSuggester/assets/HIIT/"+item+".jpg";

// Create a new Image object
var img = new Image();

// Set crossOrigin property to anonymous to allow for cross-origin requests
img.crossOrigin = "anonymous";

// Set the src attribute of the image to the image URL
img.src = imageUrl;

// Once the image is loaded, convert it to base64
img.onload = function() {
// Create a canvas element
var canvas = document.createElement('canvas');
canvas.width = img.width;
canvas.height = img.height;

// Get the canvas context
var ctx = canvas.getContext('2d');

// Draw the image onto the canvas
ctx.drawImage(img, 0, 0);

// Get the base64 representation of the image
return canvas.toDataURL('image/jpeg'); // Change 'image/jpeg' to the desired format
// Store the base64 image data in a variable or use it as needed
console.log("Base64 Image Data:", base64ImageData);
};

// If there's an error loading the image
img.onerror = function() {
console.error("Error loading image:", imageUrl);
};

}


Expand Down Expand Up @@ -565,31 +531,47 @@ function printPdf(e, i) {
if(ExDb[daysL].hasOwnProperty('HIIT'))
for (const x of ExDb[daysL]['HIIT']) {
console.log(daysL + " - " + x)

HIITImgs.columns.push([
{
margin: [0, 20, 0, 0],
image: fetchAndConvert(x),
height: 150,
width: 150
},
])
}
}
item =x
console.log(item)
var imageUrl = "https://starone01.github.io/foodSuggester/assets/HIIT/"+item+".jpg";

// Create a new Image object
var img = new Image();

let style = {
defaultStyle: {
fontSize: 8,
bold: true,
},
header: {
fontSize: 18,
bold: true,
},
};
// Set crossOrigin property to anonymous to allow for cross-origin requests
img.crossOrigin = "anonymous";

// Set the src attribute of the image to the image URL
img.src = imageUrl;

let d = new Date()
// Once the image is loaded, convert it to base64
img.onload = function() {
// Create a canvas element
var canvas = document.createElement('canvas');
canvas.width = img.width;
canvas.height = img.height;

// Get the canvas context
var ctx = canvas.getContext('2d');

// Draw the image onto the canvas
ctx.drawImage(img, 0, 0);
let base64ImageData= canvas.toDataURL('image/jpeg')
console.log("Base64 Image Data:", base64ImageData);
// Get the base64 representation of the image
HIITImgs.columns.push([
{
text: daysL
},
{
margin: [0, 20, 0, 0],
image: base64ImageData,
height: 150,
width: 150
},
]) // Change 'image/jpeg' to the desired format
// Store the base64 image data in a variable or use it as needed
let d = new Date()
let dateDb = [ d.getDate(),d.getMonth(),d.getFullYear(),d.getHours(),d.getHours(),d.getMinutes()]
let nameO = `${nameval}-${PhNo}//${nameval}-${PhNo}_${dateDb[0]}-${dateDb[1]}-${dateDb[2]}_${dateDb[3]}-${dateDb[4]}` ;
if(i === 0 || i===2) {
Expand All @@ -606,7 +588,30 @@ let nameO = `${nameval}-${PhNo}//${nameval}-${PhNo}_${dateDb[0]}-${dateDb[1]}-${
DataDef.styles = style;
pdfMake.createPdf(DataDef).download(nameO);
nameO+= '-Plan.pdf'
return [DataDef,nameO, PhNo];
return [DataDef,nameO, PhNo];
};

// If there's an error loading the image
img.onerror = function() {
console.error("Error loading image:", imageUrl);
};

}
}


let style = {
defaultStyle: {
fontSize: 8,
bold: true,
},
header: {
fontSize: 18,
bold: true,
},
};


// console.log(blobO)
// return [blobO, nameO];
};

0 comments on commit bf41fca

Please sign in to comment.