- Creazione di un Nuovo Catalogo (e la sua relativa pagina), a partire da un modello ed un catalogo esistente.
- Aggiunta dei bottoni sul Ccatalogocard
This commit is contained in:
@@ -33,7 +33,6 @@ import { CContainerCatalogoCard } from '@src/components/CContainerCatalogoCard';
|
||||
import { CSelectUserActive } from '@src/components/CSelectUserActive';
|
||||
|
||||
import html2pdf from 'html2pdf.js';
|
||||
import { PDFDocument } from 'pdf-lib';
|
||||
import { saveAs } from 'file-saver';
|
||||
|
||||
import type {
|
||||
@@ -1118,7 +1117,7 @@ export default defineComponent({
|
||||
if (!salva_listatemp) {
|
||||
rigeneraLibri.value = false;
|
||||
} else {
|
||||
tabgen.value = 'generato'
|
||||
tabgen.value = 'generato';
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1370,7 +1369,7 @@ export default defineComponent({
|
||||
|
||||
const trovatocatalogo = myCatalog.value;
|
||||
|
||||
let arrGeneraleProdotti = [];
|
||||
let arrGeneraleProdotti: IProduct[] = [];
|
||||
|
||||
if (usaprodottiSalvati && myCatalog.value?.lista_prodotti?.length > 0) {
|
||||
} else {
|
||||
@@ -1380,6 +1379,8 @@ export default defineComponent({
|
||||
let indprod = 0;
|
||||
const indprodGenerale = 0;
|
||||
|
||||
let indscheda = -1;
|
||||
|
||||
let indtotale = 0;
|
||||
|
||||
let arrprod = [];
|
||||
@@ -1387,6 +1388,7 @@ export default defineComponent({
|
||||
let schedaprec = null;
|
||||
|
||||
for (const recscheda of optcatalogo.value.arrSchede!) {
|
||||
indscheda++;
|
||||
if (recscheda && recscheda.scheda) {
|
||||
const schedePerRiga = recscheda.scheda.numschede_perRiga || 1;
|
||||
const schedePerCol = recscheda.scheda.numschede_perCol || 1;
|
||||
@@ -1458,8 +1460,10 @@ export default defineComponent({
|
||||
if (optcatalogo.value.maxnumlibri! > 0) {
|
||||
if (indtotale > optcatalogo.value.maxnumlibri!) break;
|
||||
} else {
|
||||
// if (indtotale > 1000)
|
||||
// break
|
||||
if (indscheda > 10000) {
|
||||
lastresultend = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1605,6 +1609,16 @@ export default defineComponent({
|
||||
}
|
||||
}
|
||||
|
||||
// Se la lista è vuota allora mostra il Tab Lista:
|
||||
if (myCatalog.value && !showListaFiltrata.value) {
|
||||
if (myCatalog.value.lista_prodotti.length === 0) {
|
||||
tabcatalogo.value = 'lista'
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
optrigenera.value.visibilitaDisp = tools.getCookie(
|
||||
(showListaFiltrata.value ? 'INC_ES_' : '') + 'VIS_DISP',
|
||||
costanti.DISP.DISPONIBILI
|
||||
@@ -1800,7 +1814,7 @@ export default defineComponent({
|
||||
};
|
||||
|
||||
function groupedPages(recscheda: ISchedaSingola) {
|
||||
return recscheda.arrProdToShow;
|
||||
return recscheda.arrProdToShow?.slice(0, 50);
|
||||
}
|
||||
function generateStyleCatalogo(optcatalogo: IOptCatalogo) {
|
||||
return {};
|
||||
@@ -2328,7 +2342,8 @@ export default defineComponent({
|
||||
myCatalog.value.lista_prodotti.length !== arr.length ||
|
||||
!myCatalog.value.lista_prodotti.every(
|
||||
(prod, index) => prod._id === arr[index]._id
|
||||
) || aggiorna
|
||||
) ||
|
||||
aggiorna
|
||||
) {
|
||||
myCatalog.value.lista_prodotti = [...arr];
|
||||
|
||||
@@ -2430,128 +2445,6 @@ export default defineComponent({
|
||||
optcatalogo.value.areadistampa!.scalex = 1;
|
||||
optcatalogo.value.areadistampa!.scaley = 1;
|
||||
};
|
||||
|
||||
async function generateLargePDF(
|
||||
instampa: boolean,
|
||||
opt: any,
|
||||
pagesSelector = '.page',
|
||||
salvasufiledascaricare: boolean,
|
||||
dir_out: string,
|
||||
file_out: string
|
||||
) {
|
||||
const pages = document.querySelectorAll(pagesSelector);
|
||||
const pdfs = [];
|
||||
|
||||
const TESTPAO = false;
|
||||
|
||||
let primapagina = 0;
|
||||
let numpagine = pages.length;
|
||||
|
||||
if (TESTPAO) {
|
||||
primapagina = 2;
|
||||
numpagine = 1;
|
||||
}
|
||||
|
||||
try {
|
||||
for (let i = primapagina; i < numpagine; i++) {
|
||||
const page = pages[i];
|
||||
|
||||
// Nascondi altre pagine
|
||||
pages.forEach((p) => p.classList.add('hidden'));
|
||||
page.classList.remove('hidden');
|
||||
|
||||
await new Promise((resolve) => setTimeout(resolve, 100));
|
||||
|
||||
// Genera singolo PDF
|
||||
const pdfBlob = await new Promise<Blob | null>((resolve) => {
|
||||
html2pdf()
|
||||
.set(opt)
|
||||
.from(page)
|
||||
.toPdf()
|
||||
.output('blob', { compress: true })
|
||||
.then(resolve)
|
||||
.catch(() => resolve(null));
|
||||
});
|
||||
|
||||
if (pdfBlob) {
|
||||
pdfs.push(pdfBlob);
|
||||
}
|
||||
}
|
||||
|
||||
// Unisci PDF
|
||||
const finalPdf = await PDFDocument.create();
|
||||
for (const blob of pdfs) {
|
||||
const pdf = await PDFDocument.load(await blob.arrayBuffer());
|
||||
const copiedPages = await finalPdf.copyPages(pdf, pdf.getPageIndices());
|
||||
copiedPages.forEach((p) => finalPdf.addPage(p));
|
||||
}
|
||||
|
||||
const finalPdfBytes = await finalPdf.save(); // Uint8Array
|
||||
|
||||
// ✅ Conversione in Blob
|
||||
const blob = new Blob([finalPdfBytes], { type: 'application/pdf' });
|
||||
|
||||
// ✅ Download
|
||||
if (salvasufiledascaricare) {
|
||||
saveAs(blob, opt.filename);
|
||||
} else {
|
||||
// ✅ Crea un oggetto File da Blob
|
||||
const pdfFile = new File([blob], 'report.pdf', {
|
||||
type: 'application/pdf',
|
||||
});
|
||||
|
||||
// Converti il file appena generato
|
||||
const ris = await globalStore.convertPdf(
|
||||
pdfFile,
|
||||
salvasufiledascaricare,
|
||||
widthpdf.value,
|
||||
heightpdf.value,
|
||||
optcatalogo.value.areadistampa.compress
|
||||
? instampa
|
||||
? compressionepdf.value
|
||||
: 'printer'
|
||||
: '',
|
||||
instampa,
|
||||
dir_out,
|
||||
file_out,
|
||||
true,
|
||||
optcatalogo.value
|
||||
);
|
||||
|
||||
if (ris) {
|
||||
const catalog = myCatalog.value;
|
||||
if (catalog) {
|
||||
if (instampa) {
|
||||
catalog.pdf_generato_stampa = ris.fileout;
|
||||
catalog.pdf_generato_stampa_compressed = ris.fileout_compressed;
|
||||
|
||||
catalog.pdf_generato_stampa_size = ris.filesize;
|
||||
catalog.pdf_generato_stampa_compr_size = ris.filesize_compressed;
|
||||
|
||||
catalog.data_generato_stampa = tools.getDateNow();
|
||||
} else {
|
||||
catalog.pdf_generato_compressed = ris.fileout_compressed;
|
||||
catalog.pdf_generato = ris.fileout;
|
||||
|
||||
catalog.pdf_generato_size = ris.filesize;
|
||||
catalog.pdf_generato_compr_size = ris.filesize_compressed;
|
||||
|
||||
catalog.data_generato = tools.getDateNow();
|
||||
}
|
||||
await saveCatalog();
|
||||
} else {
|
||||
strout.value = JSON.stringify(ris, null, 2);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Ripristina visibilità
|
||||
pages.forEach((p) => p.classList.remove('hidden'));
|
||||
} catch (e) {
|
||||
console.error('Err', e.message);
|
||||
}
|
||||
}
|
||||
|
||||
// Conversione da unità a pixel, con base 96 DPI
|
||||
function unitToPx(val: number, unit: string): number {
|
||||
const dpi = shared_consts.PUNTI_PER_POLLICE;
|
||||
@@ -2645,115 +2538,6 @@ export default defineComponent({
|
||||
}
|
||||
}
|
||||
|
||||
const generatePDF = async () => {
|
||||
await nextTick();
|
||||
|
||||
const instampa = isStampa.value;
|
||||
const msg = instampa ? 'stampa' : '';
|
||||
|
||||
console.log(`1) Generazione PDF ${msg} in corso, attendere ...`);
|
||||
$q.loading.show({
|
||||
message: `Generazione PDF ${msg} in corso, attendere ...`,
|
||||
});
|
||||
|
||||
try {
|
||||
const defaultMargin = 0;
|
||||
const area = optcatalogo.value.areadistampa!;
|
||||
const unit = area.unit;
|
||||
const orientation = area.orientation;
|
||||
const compress = area.compress;
|
||||
|
||||
let originalFormat =
|
||||
(instampa ? area.format_printable : area.format) || A4_FORMAT_MM;
|
||||
const scalex = tools.getScaleX(optcatalogo.value, null);
|
||||
const scaley = tools.getScaleY(optcatalogo.value, null);
|
||||
const scalecanvas = area.scalecanvas;
|
||||
|
||||
const pagina = optcatalogo.value.dimensioni_def.pagina;
|
||||
|
||||
// Calcola dimensioni effettive in mm (ajustate)
|
||||
//const formatWidthMm = tools.pxToMm(pagina.size.width) * scalex * scalecanvas;
|
||||
//const formatHeightMm = tools.pxToMm(pagina.size.height) * scaley * scalecanvas;
|
||||
|
||||
const formatWidthMm = originalFormat[0];
|
||||
const formatHeightMm = originalFormat[1];
|
||||
|
||||
let mioformato = [formatWidthMm * scalex, formatHeightMm * scaley];
|
||||
|
||||
// Calcola dimensioni del PDF in pixel
|
||||
const pdfWidthPx = unitToPx(originalFormat[0], unit);
|
||||
const pdfHeightPx = unitToPx(originalFormat[1], unit);
|
||||
|
||||
// Ottieni contenuto HTML
|
||||
const element = document.querySelector('.pdf-section');
|
||||
if (!element) throw new Error('Elemento pdf-content non trovato');
|
||||
|
||||
const rect = element.getBoundingClientRect();
|
||||
const contentWidthPx = rect.width * scalex;
|
||||
const contentHeightPx = rect.height * scaley;
|
||||
|
||||
const scale2 = getBestFitScale(rect.width, rect.height, pdfWidthPx, pdfHeightPx);
|
||||
|
||||
// Se formato non valido, fallback ad A4
|
||||
const myfile = getPdfFilename() + '.pdf';
|
||||
|
||||
const opt = {
|
||||
margin: [defaultMargin, defaultMargin, defaultMargin, defaultMargin],
|
||||
filename: myfile,
|
||||
image: {
|
||||
type: 'jpeg',
|
||||
quality: 0.98,
|
||||
},
|
||||
html2canvas: {
|
||||
scale: scalecanvas,
|
||||
useCORS: true,
|
||||
letterRendering: true,
|
||||
},
|
||||
jsPDF: {
|
||||
unit,
|
||||
format: mioformato,
|
||||
orientation,
|
||||
compress,
|
||||
},
|
||||
enableLinks: true,
|
||||
pagebreak: { mode: 'avoid-all', before: '.card-page' },
|
||||
};
|
||||
|
||||
console.log(
|
||||
`📐 PDF: ${mioformato[0]} x ${mioformato[1]} mm | scalaX: ${scalex} scalaY: ${scaley}`,
|
||||
opt
|
||||
);
|
||||
|
||||
await generateLargePDF(
|
||||
instampa,
|
||||
opt,
|
||||
'.pdf-section',
|
||||
false,
|
||||
'upload/cataloghi/',
|
||||
myfile
|
||||
);
|
||||
|
||||
optcatalogo.value.generazionePDFInCorso = false;
|
||||
optcatalogo.value.areadistampa!.scalex = 1;
|
||||
optcatalogo.value.areadistampa!.scaley = 1;
|
||||
|
||||
$q.loading.hide();
|
||||
$q.notify({
|
||||
color: 'positive',
|
||||
message: 'PDF generato con successo!',
|
||||
icon: 'check',
|
||||
});
|
||||
} catch (error) {
|
||||
$q.loading.hide();
|
||||
$q.notify({
|
||||
color: 'negative',
|
||||
message: 'Errore nella generazione del PDF',
|
||||
icon: 'error',
|
||||
});
|
||||
console.error('Errore nella generazione del PDF:', error);
|
||||
}
|
||||
};
|
||||
|
||||
// Default formato A4 in mm
|
||||
const A4_FORMAT_MM = [210, 297];
|
||||
|
||||
@@ -2991,7 +2775,6 @@ export default defineComponent({
|
||||
preparePDFStampa,
|
||||
terminaPDF,
|
||||
toggleDebug,
|
||||
generatePDF,
|
||||
getPdfFilename,
|
||||
filtroStrApplicato,
|
||||
getCollane,
|
||||
|
||||
Reference in New Issue
Block a user