Question:
How to extract data and save it?
Product:
Wix Editor
What are you trying to achieve:
Hello everyone
Thanks to hp1’s topic (Generate and download a custom PDF with pdf-lib), I managed to create PDFs from one of my pages (Customer Invoice)with pdf-lib. It works perfectly. I can extract my customer data (first and last name, invoice amount, tax, adress, etc.) and create my PDF. Thank you again.
However, I’m facing some difficulties:
1/ How can I export my dataset data to this PDF? (Multiple data): My client has several services : how to export them? (A table?)
2/ How can I embed an image? (.src?) (take look at > //LOGO section : i would like to insert the source of my logo: actually i use a model of pdf-lib
3/ I created a collection called “Mes Factures” with a text field “ResaNum” and a document field “Invoice Name”. My PDF is currently downloaded and saved on my computer and in WIX’s “Media Collection.”
How do I integrate my PDF document generated and saved from “Facture client” page into my “Mes Factures” collection? (Actyually i import it manually : but i would like to do it with a code automatically)
Thank you all for any help you can provide.
Here the code i use to generate and save my pdf :
$w(‘#GenererFacture’).onClick(async(event) => {
> // Nom FICHIER
const fileName =$w('#text235').text+" "+$w('#text4').text;
*> // Nouveau PDF
const pdfDoc = await PDFDocument.create();
> // POLICE
const timesRomanFont = await pdfDoc.embedFont(StandardFonts.TimesRoman); // Add a blank page to the document const page = pdfDoc.addPage(); // Get the width and height of the page const { width, height } = page.getSize();
> //LOGO
const jpgUrl = ‘https://pdf-lib.js.org/assets/cat_riding_unicorn.jpg’
//const jpgUrl = ‘file:///Users/user1/Desktop/Logo.jpg’
//const jpgUrl=$w(‘#logo’).src
const jpgImageBytes = await fetch(jpgUrl).then((res) => res.arrayBuffer())const jpgImage = await pdfDoc.embedJpg(jpgImageBytes)
const jpgDims = jpgImage.scale(0.5)page.drawImage(jpgImage, {
x: 10,
y: 720,
width: 90,
height: 90,
//rotate: degrees(30),
opacity: 0.75,
})
> //ENTETE GAUCHE
page.drawText(“CAMPING LE PRE DU ROI”, { x: 10, y: 720, size: 12, font: timesRomanFont })
page.drawText(“Route des Bouchets”, { x: 10, y: 700, size: 12, font: timesRomanFont })
page.drawText(“89 660 MAILLY LE CHATEAU”, { x: 10, y: 680, size: 12, font: timesRomanFont })
page.drawText(“03.86.81.44.37”, { x: 10, y: 660, size: 12, font: timesRomanFont })
page.drawText(“camping-maillylechateau@orange.fr”, { x: 10, y: 650, size: 12, font: timesRomanFont})
> //ENTETE DROIT
page.drawText($w(‘#text235’).text, { x: 420, y: 800, size: 12, font: timesRomanFont })
page.drawText($w(‘#text4’).text, { x: 480, y: 800, size: 12, font: timesRomanFont })
page.drawText($w(‘#text252’).text, { x: 420, y: 780, size: 12, font: timesRomanFont })
page.drawText($w(‘#text241’).text, { x: 420, y: 760, size: 12, font: timesRomanFont })
page.drawText($w(‘#text242’).text, { x: 480, y: 760, size: 12, font: timesRomanFont})
> // TRAIT DE SEPARATION HAUT (NUM FACTURE)
page.drawLine({
start:{ x: 10, y: 630, },
end:{ x: 550, y: 630, },
thickness: 2,
//color: rgb(0.75, 0.2, 0.2),
opacity: 0.75,
})
> //NUM FACTURE
page.drawText(“VOTRE FACTURE N°…”+ $w(‘#text240’).text, { x: 45, y: 605, size: 16, font: timesRomanFont})
> // TRAIT DE SEPARATION BAS (NUM FACTURE)
page.drawLine({
start:{ x: 10, y: 580, },
end:{ x: 550, y: 580, },
thickness: 2,
//color: rgb(0.75, 0.2, 0.2),
opacity: 0.75,
})
> //DETAILS FACTURE
page.drawText(“PRESTATIONS FACTUREES”, { x: 10, y: 500, size: 15, font: timesRomanFont})
> // TRAIT DE SEPARATION BAS DE PAGE
page.drawLine({
start:{ x: 10, y: 205, },
end:{ x: 550, y: 205, },
thickness: 1,
//color: rgb(0.75, 0.2, 0.2),
opacity: 0.75,
})
> // TOTAL FACTURE A REGLER BAS DE PAGE
page.drawText("TOTAL HT ", { x: 10, y: 180, size: 12, font: timesRomanFont })
page.drawText($w(‘#input12’).value+“€”, { x: 480, y: 180, size: 12, font: timesRomanFont })
page.drawText(“T.V.A”, { x: 10, y: 160, size: 12, font: timesRomanFont })
page.drawText($w(‘#input13’).value+“€”, { x: 480, y: 160, size: 12, font: timesRomanFont })
page.drawText(“Sous-Total TTC”, { x: 10, y: 140, size: 12, font: timesRomanFont })
page.drawText($w(‘#input15’).value+“€”, { x: 480, y: 140, size: 15, font: timesRomanFont,color:rgb(1,0,0) })
> // TRAIT DE SEPARATION BAS DE PAGE
page.drawLine({
start:{ x: 475, y: 125, },
end:{ x:550, y: 125, },
thickness: 1,
//color: rgb(0.75, 0.2, 0.2),
opacity: 0.75,
})page.drawText("Réductions (Séjour long & Groupe) ", { x: 10, y: 100, size: 12, font: timesRomanFont })
page.drawText($w(‘#input20’).value+“€”, { x: 480, y: 100, size: 12, font: timesRomanFont })
page.drawText("Taxes de séjour ", { x: 10, y: 80, size: 12, font: timesRomanFont })
page.drawText($w(‘#input17’).value+“€”, { x: 480, y: 80, size: 12, font: timesRomanFont })
> // TRAIT DE SEPARATION BAS DE PAGE
page.drawLine({
start:{ x: 475, y: 60, },
end:{ x:550, y: 60, },
thickness: 1,
//color: rgb(0.75, 0.2, 0.2),
opacity: 0.75,
})page.drawText("TOTAL A REGLER: ", { x: 10, y: 40, size: 12, font: timesRomanFont })
page.drawText($w(‘#input18’).value+“€”, { x: 480, y: 40, size: 15, font: timesRomanFont,color:rgb(1,0,0) })// Serialize the PDFDocument to Base64 const pdfDataUri = await pdfDoc.saveAsBase64({ dataUri: true }); const dataUriPrefix = 'data:application/pdf;base64,'; const base64 = pdfDataUri.slice(dataUriPrefix.length);
> // SAUVEGARDE ET DL DU FICHIER
savePDF(base64, fileName) .then(downloadUrl => { console.log(downloadUrl); wixLocation.to(downloadUrl); }) .catch((error) => { console.error("Erreur pendant l'enregistrement du PDF: ", ERREUR); });
})