|
TS:
// Required modules for exporting
import { PdfDocument, PdfPage, PdfBitmap, RectangleF } from '@syncfusion/ej2-pdf-export';
class Main {
public drawImage(imageString: string): void {
imageString = imageString.slice(imageString.indexOf(',') + 1);
// create a new PDF document
let document: PdfDocument = new PdfDocument();
// add a page to the document
let page1: PdfPage = document.pages.add();
// create a base64 string of the JPEG image
/* tslint:disable */
// load the image from the base64 string of original image
let image: PdfBitmap = new PdfBitmap(imageString);
// draw the image
page1.graphics.drawImage(image, 0, 0, 300, 300);
/* tslint:enable */
// save the document
document.save('output.pdf');
// destroy the document
document.destroy();
}
};
document.getElementById('togglebtn').onclick = () => {
let fileName: string = (<HTMLInputElement>(document.getElementById('fileName'))).value;
if (mode.value == 'PDF') {
// Draw chart as an image element
let element: HTMLCanvasElement = <HTMLCanvasElement>createElement('canvas', {
id: 'ej2-canvas',
attrs: {
'width': chart1.availableSize.width.toString(),
'height': chart1.availableSize.height.toString()
}
});
// ...
let url: string = window.URL.createObjectURL(
new Blob(
[(new XMLSerializer()).serializeToString(chart1.svgObject)],
{ type: 'image/svg+xml' }
)
);
let image: HTMLImageElement = new Image();
let ctx: CanvasRenderingContext2D = element.getContext('2d');
image.onload = (() => {
ctx.drawImage(image, 0, 0);
window.URL.revokeObjectURL(url);
// Converting canvas element to string
let imageString: string = element.toDataURL('image/jpeg').replace('image/jpeg', 'image/octet-stream');
let obj: Main = new Main();
obj.drawImage(imageString);
});
image.src = url;
}
};
|