Search code examples
jspdf

Invalid arguments passed to jsPDF.text at E.y.private.text.y.text


First time poster, have usually been able to work every problem out myself. I'm at a loss on this one.

I have had almost this exact code snippet work on another project but this time it works at times and others it doesn't

If needed I can post the rest of the project. Or I'll make a sandbox and post the link when the wife goes to bed.

 import jsPDF from 'jspdf'
    const doc = new jsPDF();
  
    const img = '../../assets/AADONOTDELETEused (2).jpg'
  
    var width = doc.internal.pageSize.getWidth()
    var height = doc.internal.pageSize.getHeight()
    doc.addImage(img, 'JPEG', 0, 0, width, height);
  
    // Set the font size and text alignment for form field values
    doc.setFontSize(9);
  
    // Fill in the form field values
    doc.text(formData.name, 42, 50); // Name field
    doc.text(formData.phone, 136, 50); // Phone field
    doc.text(formData.email, 100, 60); // Email field
    doc.text(formData.address, 31, 70); // Address field
    doc.text(formData.year, 12, 92); // Year field
    doc.text(formData.model, 12, 108); // Accessories field
    doc.text(formData.stockNum, 42, 139); // StockNum field
    doc.text(formData.trade, 22, 180); // Trade field
    doc.text(formData.deposit, 31, 163); // Deposit field
    doc.text(formData.options, 15, 211); // Accessories field
    doc.text(formData.msrp, 118, 98); // Accessories field
    doc.text(formData.freight, 188, 85); // Accessories field
    doc.text(formData.commodity, 188, 93); // Accessories field
    doc.text(formData.pdi, 188, 101); // Accessories field
  
    // Save the filled PDF document
    doc.save(("{formData.name}.pdf"));
  };

I have tried taking each of the inputs and test them seperatly, maybe it was the data being passed. Even that does't show me what exactly is wrong because I'll take the name field out and it works or if left in, it works. I'm obviously missing something, I've went over the docs, google, other peoples projects hopefully some stackover wizard could help me out on this.

ERROR Invalid arguments passed to jsPDF.text at E.y.private.text.y.text


Solution

  • Was at work with nothing to do and started again to go over other people's projects who implemented this lib, and saw different syntax being used and just gave it a shot. I don't know why this worked, because my original code worked in other projects. If you get stuck at the same place I was I hope this helps. The trigger used; onClick={ModifyPdf}

    `
    function ModifyPdf() {

    const doc = new jsPDF();
    
    const img = 'data:image/jpeg;base64,/9j/4AAQSkZJRg clipped for obvious reasons'
    
    var width = doc.internal.pageSize.getWidth()
    var height = doc.internal.pageSize.getHeight()
    doc.addImage(img, 'JPEG', 0, 0, width, height);
    
    // Set the font size and text alignment for form field values
    doc.setFontSize(9);
    
    // Fill in the form field values
    doc.text(42, 50, `${formData.name}`); // Name field
    doc.text( 136, 50, `${formData.phone}`); // Phone field
    doc.text( 100, 60, `${formData.email}`); // Email field
    doc.text( 31, 70, `${formData.address}`); // Address field
    doc.text( 12, 92, `${formData.year}`); // Year field
    doc.text( 12, 108, `${formData.model}`); // Accessories field
    doc.text( 42, 139, `${formData.stockNum}`); // StockNum field
    doc.text( 22, 180, `${formData.trade}`); // Trade field
    doc.text( 31, 163, `${formData.deposit}`); // Deposit field
    doc.text( 15, 211, `${formData.options}`); // Accessories field
    doc.text( 118, 98, `${fetchedData.msrp}`); // Accessories field
    doc.text( 188, 85, `${fetchedData.freight}`); // Accessories field
    doc.text( 188, 93, `${fetchedData.commodity}`); // Accessories field
    doc.text( 188, 101, `${fetchedData.pdi}`); // Accessories field
    
    // Save the filled PDF document
    doc.save((`${formData.name}.pdf`));
    

    }; `