Как мне добавить несколько изображений base64 в PDF-файл с помощью JSPDF?

#javascript #jspdf

#javascript #jspdf

Вопрос:

У меня проблема, когда я пытаюсь добавить несколько изображений в файл PDF. Я не могу найти решение, кто может мне помочь…

Спасибо!

Мой код!

 import './App.css';
import axios from 'axios';
import { useState } from 'react';
import jsPDF from 'jspdf';
import 'jspdf-autotable';

function App() {

  const [products, setProducts] = useState([])

  let doc = new jsPDF()

  function createCatalog() {
    doc.setFontSize(40)
    doc.text(35, 25, 'Paranyan loves jsPDF')
    doc.addImage(`data:image/png;base64, ${products[0].image}`, 'PNG', (1 * 100), 10, 70, 15);
    doc.addImage(`data:image/png;base64, ${products[1].image}`, 'PNG', (2 * 100), 10, 70, 15);
    doc.save('catalogo.pdf')
  }

  async function generateTable() {
    const res = await axios.get('http://localhost:8080');
    let list = [];
    res.data.forEach(item => {
      if(item.codigo) {
        list.push(item)
      }
    })
    setProducts(list);
  }

  return (
    <div className="App">
      <button onClick={ generateTable }>Gerar Tabela</button>
      <button onClick={ createCatalog }>Gerar Catálogo</button>
      <table id="catalog">
        <thead>
          <tr>
            <td>Foto</td>
            <td>Código</td>
            <td>Produto</td>
            <td>Preço</td>
          </tr>
        </thead>
        <tbody>
          {
            products.map(item => 
              <tr key={item.codigo}>
                <td><img src={`data:image/png;base64, ${item.image}`}></img></td>
                <td>{item.codigo}</td>
                <td>{item.produto}</td>
                <td>{item.preco}</td>
              </tr>
            ) 
          }
      
        </tbody>
      </table>
    </div>
  );
}

export default App;
  

Список моих продуктов:

 [{"categoria":"HIDRÁULICA","produto":"Luva Lisa 3/4 Marrom","codigo":"1","preco":0.32,"image":"UklGRjwEAABXRUJQVlA4WAoAAAAQAAAAigAAigAAQUxQSEEAAAAVX6CojRSIq7zwrwmJERGxpD8J2EaS1KhNIAuZArj45J/VmS8i h8LAAAAAAAAAADyjFFujHR /x9Sv/81RtkxMgBWUDgg1AMAALAQAJ0BKosAiwA kUifSzOmMSajlxkAsNIJZ0iq8q7Q/EQ2h2h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gP RlnzQLb8nflvWi2B6BXX5/hpOybGZUoq ep37Ww6eYZIOJAb4WS15hXXVZpDtYOlP/dQq8A7OUk3rYqFnXX8lwHDglOBq8xrGyaK/xfFldam6iDq CR7FCLD0qLIpxUjO/T eC2QuMH8l y0UGdx/r5l0 XHzvpn2Y0Ix12mt/KNEJiAzeQulXki70kJfk08vk FffS3KAF2c1 tbuhFNqKKXAn8FPhcXXyXf x7qoT5ga3uYHvfb2HC/wMZO95oJ3YJqlMNlJcjKoH1mVn8W6LZZnwxwoBIDg3x4M1Uz5NvNK979fYXX9M40qBsYP0 tvheo75RHNPHrTmll6PTdroD8jkHFUYY3PwUlff/n9hRsjcJPx2i2eh4/3vwTSn9oTYouz7vQDSjVfnSP38zDyMnN89CulSVNFYB6EvHEAB04AAIjmNqugwF4K0nP 6f0f4z9vw0nZNjLb5zhobjSK4Ge v gx2dD0fn/9bjJ14t9/xogdek0MR5eRn80LAT8uF0lPNHfEiMVbfmElP3/tP9T/hTEfXJNUAS3k NwHDb1m1KWn8S GXU8lj3JCIwurdrnxHcCpqoKI8DFECIz2yfXd/o39nsHRXUxbbiDUVtQSEC1TIO/kTcWjxzS faYaGfEsDgOaU NcH3vIahWyiovTTBeqXoRmDKaHsGAQzdOv oJZFohfYfY7VDNsD/D7R9 3Mjd2s7wfhESrghTiGAjqgEC4zI3YX/AAA"}]
  

Ошибка:

 jspdf.es.min.js:377 Uncaught TypeError: a.getData is not a function
    at Object.t.processWEBP (jspdf.es.min.js:377)
    at Object.P (jspdf.es.min.js:96)
    at Object.push../node_modules/jspdf/dist/jspdf.es.min.js.t.addImage (jspdf.es.min.js:96)
    at createCatalog (App.js:17)
    at HTMLUnknownElement.callCallback (react-dom.development.js:3945)
    at Object.invokeGuardedCallbackDev (react-dom.development.js:3994)
    at invokeGuardedCallback (react-dom.development.js:4056)
    at invokeGuardedCallbackAndCatchFirstError (react-dom.development.js:4070)
    at executeDispatch (react-dom.development.js:8243)
    at processDispatchQueueItemsInOrder (react-dom.development.js:8275)
    at processDispatchQueue (react-dom.development.js:8288)
    at dispatchEventsForPlugins (react-dom.development.js:8299)
    at react-dom.development.js:8508
    at batchedEventUpdates$1 (react-dom.development.js:22396)
    at batchedEventUpdates (react-dom.development.js:3745)
    at dispatchEventForPluginEventSystem (react-dom.development.js:8507)
    at attemptToDispatchEvent (react-dom.development.js:6005)
    at dispatchEvent (react-dom.development.js:5924)
    at unstable_runWithPriority (scheduler.development.js:646)
    at runWithPriority$1 (react-dom.development.js:11276)
    at discreteUpdates$1 (react-dom.development.js:22413)
    at discreteUpdates (react-dom.development.js:3756)
    at dispatchDiscreteEvent (react-dom.development.js:5889)
  

Когда я добавляю одно и то же изображение, оно работает! Но когда у меня есть 2 разных изображения, это не работает… Есть разные способы, и это не работает. Я считаю, что это не проблема с изображениями, потому что я могу отображать их в своем HTML… Я также использовал автотаблицу, и она выдает ту же ошибку.