Selaa lähdekoodia

First working implementation of Pdf generation!
Size needs to be adapted to svg. Wanted page type needs to be added (A4, letter, ..)

Matthias Uiberacker 5 vuotta sitten
vanhempi
commit
f97d7cd883

+ 2 - 1
package.json

@@ -48,11 +48,12 @@
   },
   },
   "homepage": "http://opensheetmusicdisplay.org",
   "homepage": "http://opensheetmusicdisplay.org",
   "dependencies": {
   "dependencies": {
+    "@types/vexflow": "^1.2.33",
     "es6-promise": "^4.2.5",
     "es6-promise": "^4.2.5",
     "jszip": "^3.0.0",
     "jszip": "^3.0.0",
     "loglevel": "^1.5.0",
     "loglevel": "^1.5.0",
+    "svg2pdf.js": "^1.5.0",
     "typescript-collections": "^1.1.2",
     "typescript-collections": "^1.1.2",
-    "@types/vexflow": "^1.2.33",
     "vexflow": "^1.2.89"
     "vexflow": "^1.2.89"
   },
   },
   "devDependencies": {
   "devDependencies": {

+ 4 - 0
src/MusicalScore/Graphical/VexFlow/SvgVexFlowBackend.ts

@@ -31,6 +31,10 @@ export class SvgVexFlowBackend extends VexFlowBackend {
         return this.ctx;
         return this.ctx;
     }
     }
 
 
+    public getSvgElement(): SVGElement {
+        return this.ctx.svg;
+    }
+
     public clear(): void {
     public clear(): void {
         if (!this.ctx) {
         if (!this.ctx) {
             return;
             return;

+ 20 - 0
src/OpenSheetMusicDisplay/OpenSheetMusicDisplay.ts

@@ -20,6 +20,8 @@ import { AbstractExpression } from "../MusicalScore/VoiceData/Expressions/Abstra
 import { Dictionary } from "typescript-collections";
 import { Dictionary } from "typescript-collections";
 import { NoteEnum } from "..";
 import { NoteEnum } from "..";
 import { AutoColorSet } from "../MusicalScore";
 import { AutoColorSet } from "../MusicalScore";
+import jspdf = require("jspdf-yworks/dist/jspdf.min");
+import svg2pdf = require("svg2pdf.js/dist/svg2pdf.min");
 
 
 /**
 /**
  * The main class and control point of OpenSheetMusicDisplay.<br>
  * The main class and control point of OpenSheetMusicDisplay.<br>
@@ -198,6 +200,7 @@ export class OpenSheetMusicDisplay {
         this.drawer.setZoom(this.zoom);
         this.drawer.setZoom(this.zoom);
         // Finally, draw
         // Finally, draw
         this.drawer.drawSheet(this.graphic);
         this.drawer.drawSheet(this.graphic);
+        this.createPdf((<SvgVexFlowBackend>this.drawer.Backends[0]).getSvgElement());
         if (this.drawingParameters.drawCursors && this.cursor) {
         if (this.drawingParameters.drawCursors && this.cursor) {
             // Update the cursor position
             // Update the cursor position
             this.cursor.update();
             this.cursor.update();
@@ -577,6 +580,23 @@ export class OpenSheetMusicDisplay {
         return backend;
         return backend;
     }
     }
 
 
+    public createPdf(svgElement: SVGElement): void {
+        const width: number = 1000, height: number = 1400;
+
+        // create a new jsPDF instance
+        const pdf: any = new jspdf("l", "pt", [width, height]);
+
+        // render the svg element
+        svg2pdf(svgElement, pdf, {
+            scale: 1,
+            xOffset: 0,
+            yOffset: 0
+        });
+
+        // simply save the created pdf
+        pdf.save("myPDF.pdf");
+    }
+
     //#region GETTER / SETTER
     //#region GETTER / SETTER
     public set DrawSkyLine(value: boolean) {
     public set DrawSkyLine(value: boolean) {
         if (this.drawer) {
         if (this.drawer) {