From c237987524f4dcaaf01027d253968504eae25d19 Mon Sep 17 00:00:00 2001 From: ichaabane Date: Mon, 4 Dec 2023 11:53:02 +0100 Subject: [PATCH 1/3] =?UTF-8?q?Ajouter=20les=20d=C3=A9pendances=20manques?= =?UTF-8?q?=20au=20bulid.gradle.kts=20[APPS-01WEA]?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- galite-core/build.gradle.kts | 5 ++ .../galite/visual/pivottable/UPivotTable.kt | 45 +++++++++++ .../galite/visual/pivottable/VPivotTable.kt | 77 +++++++++++++++++-- .../ui/vaadin/pivottable/DPivotTable.kt | 55 ++++++++++++- .../resources/frontend/src/generatePDF.js | 59 ++++++++++++++ .../org/kopi/galite/demo/client/ClientP.kt | 24 ++++-- 6 files changed, 248 insertions(+), 17 deletions(-) create mode 100644 galite-core/src/main/resources/META-INF/resources/frontend/src/generatePDF.js diff --git a/galite-core/build.gradle.kts b/galite-core/build.gradle.kts index 4f2ce38f4..39c078573 100644 --- a/galite-core/build.gradle.kts +++ b/galite-core/build.gradle.kts @@ -85,6 +85,11 @@ dependencies { // Pivot Table dependency implementation("org.vaadin.addons.componentfactory", "pivottable-flow", Versions.PIVOT_TABLE) + + + implementation(npm( " html2canvas ", "1.4.1")) + implementation(npm("jspdf","2.5.1")) + } dependencyManagement { diff --git a/galite-core/src/main/kotlin/org/kopi/galite/visual/pivottable/UPivotTable.kt b/galite-core/src/main/kotlin/org/kopi/galite/visual/pivottable/UPivotTable.kt index cefb9551a..7b8c291c5 100644 --- a/galite-core/src/main/kotlin/org/kopi/galite/visual/pivottable/UPivotTable.kt +++ b/galite-core/src/main/kotlin/org/kopi/galite/visual/pivottable/UPivotTable.kt @@ -19,10 +19,55 @@ package org.kopi.galite.visual.pivottable import org.kopi.galite.visual.UWindow +import org.kopi.galite.visual.chart.VPrintOptions +import java.io.IOException +import java.io.OutputStream interface UPivotTable : UWindow { /** * Builds the pivot table; */ fun build() + /** + * Exports the chart type to the PDF format. + * + * @param destination Where to write the export. + * @param options The print options. + * @throws IOException I/O errors. + */ + + @Throws(IOException::class) + fun exportToPDF(destination: OutputStream, options: VPrintOptions) + + /** + * Exports the chart type to the PDF format. + * + * @param destination Where to write the export. + * @param options The print options. + * @throws IOException I/O errors. + */ + @Throws(IOException::class) + fun exportToPDF1(destination: OutputStream, options: VPrintOptions) + + /** + * Exports the chart type to the PNG format. + * + * @param destination Where to write the export. + * @param width The image width. + * @param height The image height. + * @throws IOException I/O errors. + */ + @Throws(IOException::class) + fun exportToPNG(destination: OutputStream, width: Int, height: Int, form : String) + + /** + * Exports the chart type to the JPEG format. + * + * @param destination Where to write the export. + * @param width The image width. + * @param height The image height. + * @throws IOException I/O errors. + */ + @Throws(IOException::class) + fun exportToJPEG(destination: OutputStream, width: Int, height: Int , form : String) } diff --git a/galite-core/src/main/kotlin/org/kopi/galite/visual/pivottable/VPivotTable.kt b/galite-core/src/main/kotlin/org/kopi/galite/visual/pivottable/VPivotTable.kt index 09c7ceb98..92df8ca29 100644 --- a/galite-core/src/main/kotlin/org/kopi/galite/visual/pivottable/VPivotTable.kt +++ b/galite-core/src/main/kotlin/org/kopi/galite/visual/pivottable/VPivotTable.kt @@ -18,24 +18,27 @@ package org.kopi.galite.visual.pivottable -import java.io.File -import java.net.MalformedURLException import org.jetbrains.annotations.TestOnly - -import org.vaadin.addons.componentfactory.PivotTable.* - import org.kopi.galite.util.base.InconsistencyException import org.kopi.galite.visual.* +import org.kopi.galite.visual.chart.VPrintOptions +import org.kopi.galite.visual.dsl.common.Trigger import org.kopi.galite.visual.form.VConstants import org.kopi.galite.visual.l10n.LocalizationManager -import org.kopi.galite.visual.dsl.common.Trigger +import org.vaadin.addons.componentfactory.PivotTable.* +import java.io.File +import java.io.FileOutputStream +import java.io.IOException +import java.net.MalformedURLException /** * Represents a pivot table model. */ abstract class VPivotTable internal constructor() : VWindow(), VConstants { companion object { - + const val TYP_PDF = 1 + const val TYP_PNG = 2 + const val TYP_JPEG = 3 init { WindowController.windowController.registerWindowBuilder( org.kopi.galite.visual.Constants.MDL_PIVOT_TABLE, @@ -60,6 +63,7 @@ abstract class VPivotTable internal constructor() : VWindow(), VConstants { var interactive = Constants.MODE_INTERACTIVE val PIVOT_TABLE_Triggers = listOf(arrayOfNulls(Constants.TRG_TYPES.size)) private val activeCommands = ArrayList() + var printOptions: VPrintOptions = VPrintOptions() var help: String? = null override fun getType() = org.kopi.galite.visual.Constants.MDL_PIVOT_TABLE @@ -272,6 +276,65 @@ abstract class VPivotTable internal constructor() : VWindow(), VConstants { VHelpViewer().showHelp(genHelp()) } + /** + * Prints the report + */ + fun export(type: Int = VPivotTable.TYP_PNG) { + val ext = when (type) { + VPivotTable.TYP_PNG -> ".png" + VPivotTable.TYP_PDF -> ".pdf" + VPivotTable.TYP_JPEG -> ".jpeg" + else -> throw InconsistencyException("Export type unknown") + } + val file = FileHandler.fileHandler!!.chooseFile(getDisplay()!!, + ApplicationConfiguration.getConfiguration()!!.getDefaultDirectory(), + "chart$ext") + if (file != null) { + try { + export(file, type) + } catch (e: IOException) { + throw VExecFailedException(e) + } + } + } + + /** + * Exports the chart to the given format. + * @param file The destination file. + * @param type The export type. + * @throws IOException I/O errors. + */ + fun export(file: File, type: Int) { + val destination = FileOutputStream(file) + var exported = false + + setWaitInfo(VlibProperties.getString("export-message")) + try { + exported = when (type) { + VPivotTable.TYP_PDF -> { + (getDisplay() as UPivotTable).exportToPDF(destination, printOptions) + true + } + VPivotTable.TYP_PNG -> { + (getDisplay() as UPivotTable).exportToPDF1(destination, printOptions) + true + } + VPivotTable.TYP_JPEG -> { + (getDisplay() as UPivotTable).exportToJPEG(destination, printOptions.imageWidth, printOptions.imageHeight, "jpeg") + true + } + else -> throw InconsistencyException("Export type unknown") + } + } finally { + destination.close() + unsetWaitInfo() + if (exported) { + //fireFileProduced(file) + println(true) + } + } + } + // ---------------------------------------------------------------------- // Command // ---------------------------------------------------------------------- diff --git a/galite-core/src/main/kotlin/org/kopi/galite/visual/ui/vaadin/pivottable/DPivotTable.kt b/galite-core/src/main/kotlin/org/kopi/galite/visual/ui/vaadin/pivottable/DPivotTable.kt index 96c924ca1..a6efd8862 100644 --- a/galite-core/src/main/kotlin/org/kopi/galite/visual/ui/vaadin/pivottable/DPivotTable.kt +++ b/galite-core/src/main/kotlin/org/kopi/galite/visual/ui/vaadin/pivottable/DPivotTable.kt @@ -17,15 +17,20 @@ */ package org.kopi.galite.visual.ui.vaadin.pivottable -import org.vaadin.addons.componentfactory.PivotTable import com.vaadin.flow.component.dependency.CssImport - +import com.vaadin.flow.component.dependency.JsModule +import com.vaadin.flow.component.dependency.NpmPackage +import org.kopi.galite.visual.chart.VPrintOptions import org.kopi.galite.visual.dsl.pivottable.Dimension.Position import org.kopi.galite.visual.pivottable.MPivotTable import org.kopi.galite.visual.pivottable.UPivotTable import org.kopi.galite.visual.pivottable.VPivotTable import org.kopi.galite.visual.ui.vaadin.visual.DWindow - +import org.vaadin.addons.componentfactory.PivotTable +import java.io.OutputStream +@NpmPackage(value = "html2canvas", version = "1.4.1") +@NpmPackage(value = "jspdf", version = "2.5.1") +@JsModule("./src/generatePDF.js") @CssImport("./styles/galite/pivottable.css") class DPivotTable(private val pivotTable: VPivotTable) : DWindow(pivotTable), UPivotTable { @@ -37,6 +42,8 @@ class DPivotTable(private val pivotTable: VPivotTable) : DWindow(pivotTable), UP private val pivotOptions = PivotTable.PivotOptions() private val rows = mutableListOf() private val columns = mutableListOf() + private lateinit var pivot: PivotTable + init { getModel()!!.setDisplay(this) @@ -89,8 +96,48 @@ class DPivotTable(private val pivotTable: VPivotTable) : DWindow(pivotTable), UP PivotTable.PivotMode.NONINTERACTIVE } - val pivot = PivotTable(pivotData, pivotOptions, pivotMode) + pivot = PivotTable(pivotData, pivotOptions, pivotMode) add(pivot) } + + override fun exportToPDF(destination: OutputStream, options: VPrintOptions) { + if (::pivot.isInitialized) { + ui.get().access { + ui.get().page.executeJs("window.print()") + } + } else { + println("Pivot is not properly initialized ") + } + } + + override fun exportToPDF1(destination: OutputStream, options: VPrintOptions) { + if (::pivot.isInitialized) { + ui.get().access { + ui.get().page.executeJs("generatePDFWithId('${pivot.id.get()}');") + } + } else { + println("Pivot is not properly initialized ") + } + } + + override fun exportToPNG(destination: OutputStream, width: Int, height: Int, form: String) { + if (::pivot.isInitialized) { + ui.get().access { + ui.get().page.executeJs("generateImageWithId('${pivot.id.get()}', '$form' );"); + } + } else { + println("Pivot is not properly initialized ") + } + } + + override fun exportToJPEG(destination: OutputStream, width: Int, height: Int, form: String) { + if (::pivot.isInitialized) { + ui.get().access { + ui.get().page.executeJs("generateImageWithId('${pivot.id.get()}', '$form' );"); + } + } else { + println("Pivot is not properly initialized ") + } + } } diff --git a/galite-core/src/main/resources/META-INF/resources/frontend/src/generatePDF.js b/galite-core/src/main/resources/META-INF/resources/frontend/src/generatePDF.js new file mode 100644 index 000000000..68d1d53e3 --- /dev/null +++ b/galite-core/src/main/resources/META-INF/resources/frontend/src/generatePDF.js @@ -0,0 +1,59 @@ +import jsPDF from 'jspdf'; +import html2canvas from 'html2canvas'; + +// PDF makers +window.generatePDFWithId = function(pivotId) { + // Get the PivotTable element using the provided ID + const pivotTable = document.getElementById(pivotId); + // Check if the element with the given ID exists + if (pivotTable) { + // Use html2canvas to capture the element as an image on a canvas + html2canvas(pivotTable).then(canvas => { + //html2canvas : 21519 ms + // Create a new jsPDF instance + const doc = new jsPDF(); + // Get the canvas data as an image + const imgData = canvas.toDataURL('image/png'); + // Add the captured image of the element to the PDF + // Adjust the dimensions and positioning as needed + //addImage(imageData, format, x, y, width, height, alias, compression, rotation) + //x Coordinate (in units declared at inception of PDF document) against left edge of the page + //y Coordinate (in units declared at inception of PDF document) against upper edge of the page + doc.addImage(imgData, 'PNG', 10, 10, 170, 150); + const uniqueNumber = Math.floor(Math.random() * 1000000); // Generate a random number + const pdfFileName = `pivotTable_${uniqueNumber}.pdf`; + // Save the PDF file + doc.save(pdfFileName); + }) + } else { + console.error(`Element with ID ${pivotId} not found.`); + } +} + +//Image makers +window.generateImageWithId = function(pivotId , type) { + const pivotTable = document.getElementById(pivotId); + + if (pivotTable) { + + html2canvas(pivotTable).then(canvas => { + const resizedCanvas = document.createElement('canvas'); + const resizedContext = resizedCanvas.getContext('2d'); + resizedCanvas.width = 1000; // Set the new canvas width + resizedCanvas.height = 1000; // Set the new canvas height + resizedContext.drawImage(canvas, 0, 0, 1000, 1000); + resizedCanvas.toBlob(blob => { + const link = document.createElement('a'); + const uniqueNumber = Math.floor(Math.random() * 1000000); + const fileName = `pivotTableImage_${uniqueNumber}.${type}`; + link.href = URL.createObjectURL(blob); + link.download = fileName; + + link.click(); + URL.revokeObjectURL(link.href); + }, 'image/' + type); + }); + } else { + console.error(`Element with ID ${pivotId} not found.`); + } +} \ No newline at end of file diff --git a/galite-demo/galite-vaadin/src/main/kotlin/org/kopi/galite/demo/client/ClientP.kt b/galite-demo/galite-vaadin/src/main/kotlin/org/kopi/galite/demo/client/ClientP.kt index 52ee48f2a..ce222f8b4 100644 --- a/galite-demo/galite-vaadin/src/main/kotlin/org/kopi/galite/demo/client/ClientP.kt +++ b/galite-demo/galite-vaadin/src/main/kotlin/org/kopi/galite/demo/client/ClientP.kt @@ -16,15 +16,9 @@ */ package org.kopi.galite.demo.client -import java.util.Locale - import org.jetbrains.exposed.sql.JoinType import org.jetbrains.exposed.sql.selectAll import org.jetbrains.exposed.sql.transactions.transaction - -import org.vaadin.addons.componentfactory.PivotTable.Aggregator -import org.vaadin.addons.componentfactory.PivotTable.Renderer - import org.kopi.galite.demo.database.Client import org.kopi.galite.demo.database.Product import org.kopi.galite.demo.database.Purchase @@ -35,6 +29,10 @@ import org.kopi.galite.visual.dsl.common.Icon import org.kopi.galite.visual.dsl.form.Key import org.kopi.galite.visual.dsl.pivottable.Dimension.Position import org.kopi.galite.visual.dsl.pivottable.PivotTable +import org.kopi.galite.visual.pivottable.VPivotTable +import org.vaadin.addons.componentfactory.PivotTable.Aggregator +import org.vaadin.addons.componentfactory.PivotTable.Renderer +import java.util.* /** * Client Report @@ -51,8 +49,22 @@ class ClientP : PivotTable(title = "Clients_Pivot_Table", locale = Locale.UK) { key = Key.F1 icon = Icon.HELP } + val pdf = actor(menu = action, label = "PDF", help = "PDF Format", ident = "pdf") { + key = Key.F9 + icon = Icon.EXPORT_PDF + } + val png = actor(menu = action, label = "PNG", help = "PNG Format", ident = "png") { + key = Key.F8 + icon = Icon.PRINT + } val cmdQuit = command(item = quit) { model.close() } val helpCmd = command(item = helpForm) { model.showHelp() } + val cmdPDF = command(item = pdf) { + model.export(VPivotTable.TYP_PDF) + } + val cmdPNG = command(item = png) { + model.export(VPivotTable.TYP_PNG) + } val firstName = dimension(STRING(25), Position.NONE) { label = "First Name" From 1c75f4ff9f56f74999ece58968e8600ad56b0d2d Mon Sep 17 00:00:00 2001 From: Iyed Chaabane Date: Wed, 6 Dec 2023 11:48:29 +0100 Subject: [PATCH 2/3] Nettoyer le code [01WEA] --- galite-core/build.gradle.kts | 5 -- .../galite/visual/pivottable/UPivotTable.kt | 41 +------------ .../galite/visual/pivottable/VPivotTable.kt | 55 ++--------------- .../ui/vaadin/pivottable/DPivotTable.kt | 41 +------------ .../resources/frontend/src/generatePDF.js | 59 ------------------- .../org/kopi/galite/demo/client/ClientP.kt | 7 --- 6 files changed, 9 insertions(+), 199 deletions(-) delete mode 100644 galite-core/src/main/resources/META-INF/resources/frontend/src/generatePDF.js diff --git a/galite-core/build.gradle.kts b/galite-core/build.gradle.kts index 39c078573..4f2ce38f4 100644 --- a/galite-core/build.gradle.kts +++ b/galite-core/build.gradle.kts @@ -85,11 +85,6 @@ dependencies { // Pivot Table dependency implementation("org.vaadin.addons.componentfactory", "pivottable-flow", Versions.PIVOT_TABLE) - - - implementation(npm( " html2canvas ", "1.4.1")) - implementation(npm("jspdf","2.5.1")) - } dependencyManagement { diff --git a/galite-core/src/main/kotlin/org/kopi/galite/visual/pivottable/UPivotTable.kt b/galite-core/src/main/kotlin/org/kopi/galite/visual/pivottable/UPivotTable.kt index 7b8c291c5..ba42430cd 100644 --- a/galite-core/src/main/kotlin/org/kopi/galite/visual/pivottable/UPivotTable.kt +++ b/galite-core/src/main/kotlin/org/kopi/galite/visual/pivottable/UPivotTable.kt @@ -19,55 +19,20 @@ package org.kopi.galite.visual.pivottable import org.kopi.galite.visual.UWindow -import org.kopi.galite.visual.chart.VPrintOptions import java.io.IOException -import java.io.OutputStream interface UPivotTable : UWindow { + /** * Builds the pivot table; */ fun build() - /** - * Exports the chart type to the PDF format. - * - * @param destination Where to write the export. - * @param options The print options. - * @throws IOException I/O errors. - */ - - @Throws(IOException::class) - fun exportToPDF(destination: OutputStream, options: VPrintOptions) - - /** - * Exports the chart type to the PDF format. - * - * @param destination Where to write the export. - * @param options The print options. - * @throws IOException I/O errors. - */ - @Throws(IOException::class) - fun exportToPDF1(destination: OutputStream, options: VPrintOptions) - - /** - * Exports the chart type to the PNG format. - * - * @param destination Where to write the export. - * @param width The image width. - * @param height The image height. - * @throws IOException I/O errors. - */ - @Throws(IOException::class) - fun exportToPNG(destination: OutputStream, width: Int, height: Int, form : String) /** - * Exports the chart type to the JPEG format. + * Exports to the PDF format. * - * @param destination Where to write the export. - * @param width The image width. - * @param height The image height. * @throws IOException I/O errors. */ @Throws(IOException::class) - fun exportToJPEG(destination: OutputStream, width: Int, height: Int , form : String) + fun exportToPDF() } diff --git a/galite-core/src/main/kotlin/org/kopi/galite/visual/pivottable/VPivotTable.kt b/galite-core/src/main/kotlin/org/kopi/galite/visual/pivottable/VPivotTable.kt index 92df8ca29..99a7ada81 100644 --- a/galite-core/src/main/kotlin/org/kopi/galite/visual/pivottable/VPivotTable.kt +++ b/galite-core/src/main/kotlin/org/kopi/galite/visual/pivottable/VPivotTable.kt @@ -21,14 +21,11 @@ package org.kopi.galite.visual.pivottable import org.jetbrains.annotations.TestOnly import org.kopi.galite.util.base.InconsistencyException import org.kopi.galite.visual.* -import org.kopi.galite.visual.chart.VPrintOptions import org.kopi.galite.visual.dsl.common.Trigger import org.kopi.galite.visual.form.VConstants import org.kopi.galite.visual.l10n.LocalizationManager import org.vaadin.addons.componentfactory.PivotTable.* import java.io.File -import java.io.FileOutputStream -import java.io.IOException import java.net.MalformedURLException /** @@ -37,8 +34,7 @@ import java.net.MalformedURLException abstract class VPivotTable internal constructor() : VWindow(), VConstants { companion object { const val TYP_PDF = 1 - const val TYP_PNG = 2 - const val TYP_JPEG = 3 + init { WindowController.windowController.registerWindowBuilder( org.kopi.galite.visual.Constants.MDL_PIVOT_TABLE, @@ -63,7 +59,6 @@ abstract class VPivotTable internal constructor() : VWindow(), VConstants { var interactive = Constants.MODE_INTERACTIVE val PIVOT_TABLE_Triggers = listOf(arrayOfNulls(Constants.TRG_TYPES.size)) private val activeCommands = ArrayList() - var printOptions: VPrintOptions = VPrintOptions() var help: String? = null override fun getType() = org.kopi.galite.visual.Constants.MDL_PIVOT_TABLE @@ -279,59 +274,17 @@ abstract class VPivotTable internal constructor() : VWindow(), VConstants { /** * Prints the report */ - fun export(type: Int = VPivotTable.TYP_PNG) { - val ext = when (type) { - VPivotTable.TYP_PNG -> ".png" - VPivotTable.TYP_PDF -> ".pdf" - VPivotTable.TYP_JPEG -> ".jpeg" - else -> throw InconsistencyException("Export type unknown") - } - val file = FileHandler.fileHandler!!.chooseFile(getDisplay()!!, - ApplicationConfiguration.getConfiguration()!!.getDefaultDirectory(), - "chart$ext") - if (file != null) { - try { - export(file, type) - } catch (e: IOException) { - throw VExecFailedException(e) - } - } - } - - /** - * Exports the chart to the given format. - * @param file The destination file. - * @param type The export type. - * @throws IOException I/O errors. - */ - fun export(file: File, type: Int) { - val destination = FileOutputStream(file) - var exported = false - + fun export(type: Int ) { setWaitInfo(VlibProperties.getString("export-message")) try { - exported = when (type) { + when (type) { VPivotTable.TYP_PDF -> { - (getDisplay() as UPivotTable).exportToPDF(destination, printOptions) - true - } - VPivotTable.TYP_PNG -> { - (getDisplay() as UPivotTable).exportToPDF1(destination, printOptions) - true - } - VPivotTable.TYP_JPEG -> { - (getDisplay() as UPivotTable).exportToJPEG(destination, printOptions.imageWidth, printOptions.imageHeight, "jpeg") - true + (getDisplay() as UPivotTable).exportToPDF() } else -> throw InconsistencyException("Export type unknown") } } finally { - destination.close() unsetWaitInfo() - if (exported) { - //fireFileProduced(file) - println(true) - } } } diff --git a/galite-core/src/main/kotlin/org/kopi/galite/visual/ui/vaadin/pivottable/DPivotTable.kt b/galite-core/src/main/kotlin/org/kopi/galite/visual/ui/vaadin/pivottable/DPivotTable.kt index a6efd8862..a97124c31 100644 --- a/galite-core/src/main/kotlin/org/kopi/galite/visual/ui/vaadin/pivottable/DPivotTable.kt +++ b/galite-core/src/main/kotlin/org/kopi/galite/visual/ui/vaadin/pivottable/DPivotTable.kt @@ -18,19 +18,13 @@ package org.kopi.galite.visual.ui.vaadin.pivottable import com.vaadin.flow.component.dependency.CssImport -import com.vaadin.flow.component.dependency.JsModule -import com.vaadin.flow.component.dependency.NpmPackage -import org.kopi.galite.visual.chart.VPrintOptions import org.kopi.galite.visual.dsl.pivottable.Dimension.Position import org.kopi.galite.visual.pivottable.MPivotTable import org.kopi.galite.visual.pivottable.UPivotTable import org.kopi.galite.visual.pivottable.VPivotTable import org.kopi.galite.visual.ui.vaadin.visual.DWindow import org.vaadin.addons.componentfactory.PivotTable -import java.io.OutputStream -@NpmPackage(value = "html2canvas", version = "1.4.1") -@NpmPackage(value = "jspdf", version = "2.5.1") -@JsModule("./src/generatePDF.js") + @CssImport("./styles/galite/pivottable.css") class DPivotTable(private val pivotTable: VPivotTable) : DWindow(pivotTable), UPivotTable { @@ -44,7 +38,6 @@ class DPivotTable(private val pivotTable: VPivotTable) : DWindow(pivotTable), UP private val columns = mutableListOf() private lateinit var pivot: PivotTable - init { getModel()!!.setDisplay(this) } @@ -101,7 +94,7 @@ class DPivotTable(private val pivotTable: VPivotTable) : DWindow(pivotTable), UP add(pivot) } - override fun exportToPDF(destination: OutputStream, options: VPrintOptions) { + override fun exportToPDF() { if (::pivot.isInitialized) { ui.get().access { ui.get().page.executeJs("window.print()") @@ -110,34 +103,4 @@ class DPivotTable(private val pivotTable: VPivotTable) : DWindow(pivotTable), UP println("Pivot is not properly initialized ") } } - - override fun exportToPDF1(destination: OutputStream, options: VPrintOptions) { - if (::pivot.isInitialized) { - ui.get().access { - ui.get().page.executeJs("generatePDFWithId('${pivot.id.get()}');") - } - } else { - println("Pivot is not properly initialized ") - } - } - - override fun exportToPNG(destination: OutputStream, width: Int, height: Int, form: String) { - if (::pivot.isInitialized) { - ui.get().access { - ui.get().page.executeJs("generateImageWithId('${pivot.id.get()}', '$form' );"); - } - } else { - println("Pivot is not properly initialized ") - } - } - - override fun exportToJPEG(destination: OutputStream, width: Int, height: Int, form: String) { - if (::pivot.isInitialized) { - ui.get().access { - ui.get().page.executeJs("generateImageWithId('${pivot.id.get()}', '$form' );"); - } - } else { - println("Pivot is not properly initialized ") - } - } } diff --git a/galite-core/src/main/resources/META-INF/resources/frontend/src/generatePDF.js b/galite-core/src/main/resources/META-INF/resources/frontend/src/generatePDF.js deleted file mode 100644 index 68d1d53e3..000000000 --- a/galite-core/src/main/resources/META-INF/resources/frontend/src/generatePDF.js +++ /dev/null @@ -1,59 +0,0 @@ -import jsPDF from 'jspdf'; -import html2canvas from 'html2canvas'; - -// PDF makers -window.generatePDFWithId = function(pivotId) { - // Get the PivotTable element using the provided ID - const pivotTable = document.getElementById(pivotId); - // Check if the element with the given ID exists - if (pivotTable) { - // Use html2canvas to capture the element as an image on a canvas - html2canvas(pivotTable).then(canvas => { - //html2canvas : 21519 ms - // Create a new jsPDF instance - const doc = new jsPDF(); - // Get the canvas data as an image - const imgData = canvas.toDataURL('image/png'); - // Add the captured image of the element to the PDF - // Adjust the dimensions and positioning as needed - //addImage(imageData, format, x, y, width, height, alias, compression, rotation) - //x Coordinate (in units declared at inception of PDF document) against left edge of the page - //y Coordinate (in units declared at inception of PDF document) against upper edge of the page - doc.addImage(imgData, 'PNG', 10, 10, 170, 150); - const uniqueNumber = Math.floor(Math.random() * 1000000); // Generate a random number - const pdfFileName = `pivotTable_${uniqueNumber}.pdf`; - // Save the PDF file - doc.save(pdfFileName); - }) - } else { - console.error(`Element with ID ${pivotId} not found.`); - } -} - -//Image makers -window.generateImageWithId = function(pivotId , type) { - const pivotTable = document.getElementById(pivotId); - - if (pivotTable) { - - html2canvas(pivotTable).then(canvas => { - const resizedCanvas = document.createElement('canvas'); - const resizedContext = resizedCanvas.getContext('2d'); - resizedCanvas.width = 1000; // Set the new canvas width - resizedCanvas.height = 1000; // Set the new canvas height - resizedContext.drawImage(canvas, 0, 0, 1000, 1000); - resizedCanvas.toBlob(blob => { - const link = document.createElement('a'); - const uniqueNumber = Math.floor(Math.random() * 1000000); - const fileName = `pivotTableImage_${uniqueNumber}.${type}`; - link.href = URL.createObjectURL(blob); - link.download = fileName; - - link.click(); - URL.revokeObjectURL(link.href); - }, 'image/' + type); - }); - } else { - console.error(`Element with ID ${pivotId} not found.`); - } -} \ No newline at end of file diff --git a/galite-demo/galite-vaadin/src/main/kotlin/org/kopi/galite/demo/client/ClientP.kt b/galite-demo/galite-vaadin/src/main/kotlin/org/kopi/galite/demo/client/ClientP.kt index ce222f8b4..2f56130db 100644 --- a/galite-demo/galite-vaadin/src/main/kotlin/org/kopi/galite/demo/client/ClientP.kt +++ b/galite-demo/galite-vaadin/src/main/kotlin/org/kopi/galite/demo/client/ClientP.kt @@ -53,18 +53,11 @@ class ClientP : PivotTable(title = "Clients_Pivot_Table", locale = Locale.UK) { key = Key.F9 icon = Icon.EXPORT_PDF } - val png = actor(menu = action, label = "PNG", help = "PNG Format", ident = "png") { - key = Key.F8 - icon = Icon.PRINT - } val cmdQuit = command(item = quit) { model.close() } val helpCmd = command(item = helpForm) { model.showHelp() } val cmdPDF = command(item = pdf) { model.export(VPivotTable.TYP_PDF) } - val cmdPNG = command(item = png) { - model.export(VPivotTable.TYP_PNG) - } val firstName = dimension(STRING(25), Position.NONE) { label = "First Name" From 15a996d3b9a1c204fc8331dc69185fbe29071bfc Mon Sep 17 00:00:00 2001 From: Iyed Chaabane Date: Wed, 6 Dec 2023 18:01:16 +0100 Subject: [PATCH 3/3] Nettoyer [APPS-01WEA] --- .../galite/visual/pivottable/UPivotTable.kt | 9 ++------- .../galite/visual/pivottable/VPivotTable.kt | 18 +++++++----------- .../visual/ui/vaadin/pivottable/DPivotTable.kt | 4 +--- .../org/kopi/galite/demo/client/ClientP.kt | 6 +++--- 4 files changed, 13 insertions(+), 24 deletions(-) diff --git a/galite-core/src/main/kotlin/org/kopi/galite/visual/pivottable/UPivotTable.kt b/galite-core/src/main/kotlin/org/kopi/galite/visual/pivottable/UPivotTable.kt index ba42430cd..b086b53a0 100644 --- a/galite-core/src/main/kotlin/org/kopi/galite/visual/pivottable/UPivotTable.kt +++ b/galite-core/src/main/kotlin/org/kopi/galite/visual/pivottable/UPivotTable.kt @@ -19,20 +19,15 @@ package org.kopi.galite.visual.pivottable import org.kopi.galite.visual.UWindow -import java.io.IOException interface UPivotTable : UWindow { - /** * Builds the pivot table; */ fun build() /** - * Exports to the PDF format. - * - * @throws IOException I/O errors. + * Print to the PDF format. */ - @Throws(IOException::class) - fun exportToPDF() + fun imprimer() } diff --git a/galite-core/src/main/kotlin/org/kopi/galite/visual/pivottable/VPivotTable.kt b/galite-core/src/main/kotlin/org/kopi/galite/visual/pivottable/VPivotTable.kt index 99a7ada81..c748c6570 100644 --- a/galite-core/src/main/kotlin/org/kopi/galite/visual/pivottable/VPivotTable.kt +++ b/galite-core/src/main/kotlin/org/kopi/galite/visual/pivottable/VPivotTable.kt @@ -18,6 +18,9 @@ package org.kopi.galite.visual.pivottable +import java.io.File +import java.net.MalformedURLException + import org.jetbrains.annotations.TestOnly import org.kopi.galite.util.base.InconsistencyException import org.kopi.galite.visual.* @@ -25,8 +28,6 @@ import org.kopi.galite.visual.dsl.common.Trigger import org.kopi.galite.visual.form.VConstants import org.kopi.galite.visual.l10n.LocalizationManager import org.vaadin.addons.componentfactory.PivotTable.* -import java.io.File -import java.net.MalformedURLException /** * Represents a pivot table model. @@ -275,16 +276,11 @@ abstract class VPivotTable internal constructor() : VWindow(), VConstants { * Prints the report */ fun export(type: Int ) { - setWaitInfo(VlibProperties.getString("export-message")) - try { - when (type) { - VPivotTable.TYP_PDF -> { - (getDisplay() as UPivotTable).exportToPDF() - } - else -> throw InconsistencyException("Export type unknown") + when (type) { + TYP_PDF -> { + (getDisplay() as UPivotTable).imprimer() } - } finally { - unsetWaitInfo() + else -> throw InconsistencyException("Export type unknown") } } diff --git a/galite-core/src/main/kotlin/org/kopi/galite/visual/ui/vaadin/pivottable/DPivotTable.kt b/galite-core/src/main/kotlin/org/kopi/galite/visual/ui/vaadin/pivottable/DPivotTable.kt index a97124c31..200c2996e 100644 --- a/galite-core/src/main/kotlin/org/kopi/galite/visual/ui/vaadin/pivottable/DPivotTable.kt +++ b/galite-core/src/main/kotlin/org/kopi/galite/visual/ui/vaadin/pivottable/DPivotTable.kt @@ -94,13 +94,11 @@ class DPivotTable(private val pivotTable: VPivotTable) : DWindow(pivotTable), UP add(pivot) } - override fun exportToPDF() { + override fun imprimer() { if (::pivot.isInitialized) { ui.get().access { ui.get().page.executeJs("window.print()") } - } else { - println("Pivot is not properly initialized ") } } } diff --git a/galite-demo/galite-vaadin/src/main/kotlin/org/kopi/galite/demo/client/ClientP.kt b/galite-demo/galite-vaadin/src/main/kotlin/org/kopi/galite/demo/client/ClientP.kt index 2f56130db..0050e04db 100644 --- a/galite-demo/galite-vaadin/src/main/kotlin/org/kopi/galite/demo/client/ClientP.kt +++ b/galite-demo/galite-vaadin/src/main/kotlin/org/kopi/galite/demo/client/ClientP.kt @@ -49,13 +49,13 @@ class ClientP : PivotTable(title = "Clients_Pivot_Table", locale = Locale.UK) { key = Key.F1 icon = Icon.HELP } - val pdf = actor(menu = action, label = "PDF", help = "PDF Format", ident = "pdf") { + val imprimer = actor(menu = action, label = "PDF", help = "PDF Format", ident = "pdf") { key = Key.F9 - icon = Icon.EXPORT_PDF + icon = Icon.PRINT } val cmdQuit = command(item = quit) { model.close() } val helpCmd = command(item = helpForm) { model.showHelp() } - val cmdPDF = command(item = pdf) { + val cmdPDF = command(item = imprimer) { model.export(VPivotTable.TYP_PDF) }