DOMException: 'ShadowRoot'μμ 'adoptedStyleSheets' μμ±μ μ€μ νμ§ λͺ»νμ΅λλ€: μ¬λ¬ λ¬Έμμμ μμ±λ μ€νμΌμνΈλ₯Ό 곡μ νλ κ²μ νμ©λμ§ μμ΅λλ€.
λλ κ°μ λ¬Έμ λ₯Ό μκ³ μμ΄; κ·Έκ²μ μ²λ¦¬νλ λ°©λ²?
κ°λ 8 λ° μ΄μ¨ 4μμ μ¬μ©νλ λμΌν λ¬Έμ
κ°μ λ¬Έμ ionic4, κ·Έκ²μ ν΄κ²°νλ λ°©λ²?
λλ ionic 5μ κ°μ λ¬Έμ μ μ§λ©΄νκ³ μμ΅λλ€ ... λκ΅°κ° ν΄κ²°μ± μ μ°Ύμμ΅λκΉ?
<body>
<div id="html2canvas"></div>
<app-root></app-root>
</body>
const element = document.getElementById('html2canvas');
const targetElement = document.getElementById('target').cloneNode(true);
element.appendChild(targetElement);
this.html2canvas.html2canvas(element.firstChild).then((img) => {
this.img = img;
element.firstChild.remove();
}).catch((res) => {
console.log(res);
});
import {Injectable} from '@angular/core';
import {AlertService} from './alert.service';
declare let html2canvas;
@Injectable()
export class Html2canvasService {
constructor(private alert: AlertService) {
}
public html2canvas(ele) {
if (!ele) {
return;
}
const option = {allowTaint: true, useCORS: true};
return html2canvas(ele, option).then((canvas) => {
if (canvas) {
return canvas.toDataURL('image/png');
}
return null;
}).catch((res) => {
console.log(res);
return res;
});
}
}
@G-yanpeng μ루μ μ λ μμΈν μ€λͺ ν΄ μ£Όμκ² μ΅λκΉ? ionic 4 angular8 λ²μ μ λν΄μλ λμΌν μμ μ μλνκ³ μμ΅λλ€.
κ°μ¬ν©λλ€!
κ°μ λ¬Έμ . λκ΅°κ° ν΄κ²°μ± μ μ°Ύμμ΅λκΉ?
κ°μ¬ ν΄μ!
@G-yanpeng μ루μ μ λ μμΈν μ€λͺ ν΄ μ£Όμκ² μ΅λκΉ?Ionic 4 angular8 λ²μ μμλ λμΌν μμ μ μλνκ³ μμ΅λλ€.
κ°μ¬ν©λλ€!
Angular8μλ html2canvasκ° μ λλ‘ μλνμ§ μλ μλ‘μ΄ λ³κ²½ μ¬νμ΄ μμ΅λλ€. κ·Έλ° λ€μ μ΄ μμ μ루μ μ΄ μμ΅λλ€. html2canvasκ° κ°λ μΈλΆμμ μλνλλ‘ νκ³ , κ°λλ‘ μΈν μν₯μ νΌνκΈ° μν΄ κ°λ νλͺ©(μ± λ£¨νΈ)κ³Ό λμΌν λ μ΄μ΄μ μ΄λ―Έμ§λ₯Ό μμ±νλ λ° νμν domμ 볡μ¬ν©λλ€.
Angular8μλ html2canvasκ° μ λλ‘ μλνμ§ μλ μλ‘μ΄ λ³κ²½ μ¬νμ΄ μμ΅λλ€.κ·Έλ° λ€μ html2canvasκ° angular μΈλΆμμ μλνλλ‘ νλ μμ μ루μ
μ΄ μμ΅λλ€. κ°λμ μν₯.
- κ΅¬κΈ λ²μμΌλ‘
xxx.ts κ΅¬μ± μμ νμΌμ μ 체 μ½λλ₯Ό μ 곡ν μ μμ΅λκΉ? .pdfλ‘ λ΄λ³΄λ΄λ €κ³ ν λλ λμΌν μ€λ₯κ° λ°μνμ΅λλ€. μ΄ μ€λ₯λ₯Ό μ μΈνκ³ λ€λ₯Έ λͺ¨λ κ²μ μ μμ
λλ€. .pdf νμΌλ λ³Ό μ μμ΅λλ€.
μ΄κ²μ λ΄ μ½λμ
λλ€.
(Google λ²μκΈ°λ₯Ό μ¬μ©νμ¬ μ€κ΅μ΄λ‘ λ²μ)
`
'@angular/core'μμ {Component, OnInit} κ°μ Έμ€κΈ°;
'src/app/_pipes/wc-expenditure-calc.pipe'μμ {WcExpenditureCalcPipe} κ°μ Έμ€κΈ°';
// pdfλ‘ λ΄λ³΄λ΄κΈ°
'@angular/router'μμ {λΌμ°ν°} κ°μ Έμ€κΈ°;
'ngx-export-as'μμ {ExportAsService, ExportAsConfig} κ°μ Έμ€κΈ°;
'@ionic/angular'μμ {νλ«νΌ} κ°μ Έμ€κΈ°;
'@ionic-native/file-opener/ngx'μμ {FileOpener} κ°μ Έμ€κΈ°;
'@ionic-native/file/ngx'μμ {νμΌ} κ°μ Έμ€κΈ°;
'@ionic-native/file-transfer/ngx'μμ {FileTransfer, FileTransferObject} κ°μ Έμ€κΈ°;
@Component({
selector: 'app-wc-estimate',
templateUrl: './wc-estimate.page.html',
styleUrls: ['./wc-estimate.page.scss'],
providers: [WcExpenditureCalcPipe]
})
export class WcEstimatePage implements OnInit {
dataEntered: any;
wcEstimate: any;
// export config
storageDirectory: any;
exportAsConfig: ExportAsConfig = {
type: 'pdf', // the type you want to download
elementId: 'myDiv', // the id of html/table element
options: {
margin: 15,
jsPDF: {
orientation: 'portrait',
format: 'a4',
unit: 'mm'
},
pdfCallbackFn: this.pdfCallbackFn // to add header and footer
},
}
constructor(
public router: Router,
private exportAsService: ExportAsService,
private transfer: FileTransfer,
private file: File,
private platform: Platform,
private fileOpener: FileOpener,
private wcExpenditureCalcPipe: WcExpenditureCalcPipe
) {
this.platform.ready().then(() => {
if (!this.platform.is('cordova')) {
return false;
}
if (this.platform.is('ios')) {
this.storageDirectory = this.file.externalDataDirectory;
}
else if (this.platform.is('android')) {
this.storageDirectory = this.file.externalDataDirectory;
}
else {
return false;
}
});
}
ngOnInit() {
this.dataEntered = JSON.parse(localStorage.getItem('DataEntered'));
this.getValues(this.dataEntered);
}
getValues(dataEntered: any) {
this.wcEstimate = this.wcExpenditureCalcPipe.getWCE(dataEntered)
console.log(this.wcEstimate);
}
// export PDF
export(event) {
const fileTransfer: FileTransferObject = this.transfer.create();
this.exportAsService.get(this.exportAsConfig).subscribe((data: any) => {
const fileName = 'PReport ' + this.dataEntered.Project_Name + ' - WC Estimate.pdf';
fileTransfer.download(data, this.storageDirectory + fileName).then((entry) => {
this.fileOpener.open(entry.toURL(), 'application/pdf').then(() => {
}).catch(e => {
console.log('Error opening file', e);
});
}, (error) => {
console.log('error ' + JSON.stringify(error));
});
});
event.preventDefault();
}
pdfCallbackFn(pdf: any) {
// example to add page number as footer to every page of pdf
const noOfPages = pdf.internal.getNumberOfPages();
for (let i = 1; i <= noOfPages; i++) {
pdf.setPage(i);
pdf.text('WC Estimate', 15, 10)
pdf.text('Page ' + i + ' of ' + noOfPages, 15, pdf.internal.pageSize.getHeight() - 10);
}
}
//Edit report
editClick(event: any) {
localStorage.setItem('isEdit', 'true');
this.router.navigateByUrl('/dashboard/landing');
}
//done click
doneClick(event: any) {
this.router.navigateByUrl('/dashboard/review');
}
// exit click
exitClick(event: any) {
this.router.navigateByUrl('/dashboard/entry-point');
}
}
`
platform.ready μ΄λ²€νΈλ μ λ§ νμνμ§ μμλ°, μλλ‘μ΄λλΌλ©΄ 'λ€μ΄λ‘λ' ν΄λμ, iOSλΌλ©΄ 'λ¬Έμ' ν΄λμ μΆκ°νλ €κ³ νμ΅λλ€.
@ravimallya
html2canvasservice.service.tsμ λ΄μ©μ μ
λ°μ΄νΈνμ§λ§ λμμ΄ λμ§ μμ κ² κ°μ΅λλ€. μ΄ μμ 체κ³λ₯Ό μ¬μ©νλ©΄ μ΄λ―Έμ§κ° μ μμ μΌλ‘ μμ±λ μ μμ§λ§ μμΈκ° κ³μ λ³΄κ³ λλ€λ μ μ μ μν΄μΌ ν©λλ€.
μλ§λ JSPDF λ° html2canvasλ₯Ό μ¬μ©νμ¬ PDF μμ±μ μλν μ μμ΅λλ€.
ionic4μ Yupengμμ μ€κ° κ³ν
- index.html
<body> <div id="html2canvas"></div> <app-root></app-root> </body>
- xxx.ts
const element = document.getElementById('html2canvas'); const targetElement = document.getElementById('target').cloneNode(true); element.appendChild(targetElement); this.html2canvas.html2canvas(element.firstChild).then((img) => { this.img = img; element.firstChild.remove(); }).catch((res) => { console.log(res); });
- HTML2canvasService.service.ts
import {Injectable} from '@angular/core'; import {AlertService} from './alert.service'; declare let html2canvas; @Injectable() export class Html2canvasService { constructor(private alert: AlertService) { } public html2canvas(ele) { if (!ele) { return; } const option = {allowTaint: true, useCORS: true}; return html2canvas(ele, option).then((canvas) => { if (canvas) { return canvas.toDataURL('image/png'); } return null; }).catch((res) => { console.log(res); return res; }); } }
μ λ§ κ³ λ§μ΅λλ€! νλ₯ν ν΄κ²° λ°©λ²!
ionic4μ Yupengμμ μ€κ° κ³ν
- index.html
<body> <div id="html2canvas"></div> <app-root></app-root> </body>
- xxx.ts
const element = document.getElementById('html2canvas'); const targetElement = document.getElementById('target').cloneNode(true); element.appendChild(targetElement); this.html2canvas.html2canvas(element.firstChild).then((img) => { this.img = img; element.firstChild.remove(); }).catch((res) => { console.log(res); });
- HTML2canvasService.service.ts
import {Injectable} from '@angular/core'; import {AlertService} from './alert.service'; declare let html2canvas; @Injectable() export class Html2canvasService { constructor(private alert: AlertService) { } public html2canvas(ele) { if (!ele) { return; } const option = {allowTaint: true, useCORS: true}; return html2canvas(ele, option).then((canvas) => { if (canvas) { return canvas.toDataURL('image/png'); } return null; }).catch((res) => { console.log(res); return res; }); } }
μλΉμ€μ λ€μ ν¨μ νΈμΆμμ "ERROR TypeError: html2canvas is not a function" μ€λ₯κ° λ°μν©λλ€.
return html2canvas(ele, option).then((canvas) => {
if (canvas) {
return canvas.toDataURL('image/png');
}
return null;
})
ionic4μ Yupengμμ μ€κ° κ³ν
- index.html
<body> <div id="html2canvas"></div> <app-root></app-root> </body>
- xxx.ts
const element = document.getElementById('html2canvas'); const targetElement = document.getElementById('target').cloneNode(true); element.appendChild(targetElement); this.html2canvas.html2canvas(element.firstChild).then((img) => { this.img = img; element.firstChild.remove(); }).catch((res) => { console.log(res); });
- HTML2canvasService.service.ts
import {Injectable} from '@angular/core'; import {AlertService} from './alert.service'; declare let html2canvas; @Injectable() export class Html2canvasService { constructor(private alert: AlertService) { } public html2canvas(ele) { if (!ele) { return; } const option = {allowTaint: true, useCORS: true}; return html2canvas(ele, option).then((canvas) => { if (canvas) { return canvas.toDataURL('image/png'); } return null; }).catch((res) => { console.log(res); return res; }); } }
μ΄κ²μ μμ£Ό μ μλν©λλ€. ν΄κ²°μ± μ μ£Όμ μ κ°μ¬ν©λλ€. μ΄μ¨ κΈ°λ³Έ μμ(μ: 볡μ¬ν μμ λ΄λΆμ μ΄μ¨ ν μ€νΈ, μ΄μ¨ μμ΄μ½)μμλ μλνμ§ μμ΅λλ€. νμ§λ§ λ΄ μ΄μ¨ μμλ₯Ό htmlλ‘ λ³κ²½νμ΅λλ€. μ΄μ μλ²½νκ² μλν©λλ€.
κ°μ₯ μ μ©ν λκΈ
κ°λ 8 λ° μ΄μ¨ 4μμ μ¬μ©νλ λμΌν λ¬Έμ