capitalise and add space of ID when saving as pdf

547 Views Asked by At

I have a savingasPDF function that I'm using which targets the id of what needs to be downloaded and saved as a pdf, it also then uses its id to create a title for the pdf. the id follow camel case so are 'writtenLikeThis'. I had added

 doc.text(15, 15, ID.replace(/([A-Z])/g, ' $1').trim().capitalize()); 

which was adding space and capitalising the first letter, so 'writtenLikeThis' would appear as 'Written Like This' as the title. However its suddenly stopped working, with the error of

ID.replace(...).trim(...).capitalize is not a function

below is the complete function, I'm not sure why its no longer working but if anyone has any suggestions of how to rewrite the section that adds space and capitalizes i'd really appreciate the help.

    function saveAsPDF(ID) {
    let canvas = document.querySelector('#' + ID); 
    //creates image
    let canvasImg = canvas.toDataURL("image/png", 1.0); 
    //creates PDF from img
    let doc = new jsPDF('landscape'); 
    doc.setFontSize(12); 
    doc.text(15, 15, ID.replace(/([A-Z])/g, ' $1').trim().capitalize());  
    doc.addImage(canvasImg, 'PNG', 10, 20, 280, 150 ); 
    doc.save( ID +'.pdf');
}

to summarise I need camelCase to become Camel Case or Camel case.

UPDATE solution found here updated function below:

function saveAsPDF(ID) {
    let canvas = document.querySelector('#' + ID); 
    //creates image
    let canvasImg = canvas.toDataURL("image/png", 1.0); 
    //creates PDF from img
    let doc = new jsPDF('landscape'); 
    doc.setFontSize(12); 
    doc.text(15, 15, ID.replace(/^[a-z]|[A-Z]/g, function(v, i) {
    return i === 0 ? v.toUpperCase() : " " + v.toLowerCase()})); 
    doc.addImage(canvasImg, 'PNG', 10, 20, 280, 150 ); 
    doc.save( ID +'.pdf');
}
0

There are 0 best solutions below