Html2canvas: DOMException: 'ShadowRoot'μ—μ„œ 'adoptedStyleSheets' 속성을 μ„€μ •ν•˜μ§€ λͺ»ν–ˆμŠ΅λ‹ˆλ‹€: μ—¬λŸ¬ λ¬Έμ„œμ—μ„œ μƒμ„±λœ μŠ€νƒ€μΌμ‹œνŠΈλ₯Ό κ³΅μœ ν•˜λŠ” 것은 ν—ˆμš©λ˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€.

에 λ§Œλ“  2019λ…„ 11μ›” 04일  Β·  13μ½”λ©˜νŠΈ  Β·  좜처: niklasvh/html2canvas

DOMException: 'ShadowRoot'μ—μ„œ 'adoptedStyleSheets' 속성을 μ„€μ •ν•˜μ§€ λͺ»ν–ˆμŠ΅λ‹ˆλ‹€: μ—¬λŸ¬ λ¬Έμ„œμ—μ„œ μƒμ„±λœ μŠ€νƒ€μΌμ‹œνŠΈλ₯Ό κ³΅μœ ν•˜λŠ” 것은 ν—ˆμš©λ˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€.

κ°€μž₯ μœ μš©ν•œ λŒ“κΈ€

각도 8 및 이온 4μ—μ„œ μ‚¬μš©ν•˜λŠ” λ™μΌν•œ 문제

λͺ¨λ“  13 λŒ“κΈ€

λ‚˜λ„ 같은 문제λ₯Ό μ•ˆκ³ μžˆμ–΄; 그것을 μ²˜λ¦¬ν•˜λŠ” 방법?

각도 8 및 이온 4μ—μ„œ μ‚¬μš©ν•˜λŠ” λ™μΌν•œ 문제

같은 문제 ionic4, 그것을 ν•΄κ²°ν•˜λŠ” 방법?

λ‚˜λŠ” ionic 5와 같은 λ¬Έμ œμ— μ§λ©΄ν•˜κ³  μžˆμŠ΅λ‹ˆλ‹€ ... λˆ„κ΅°κ°€ 해결책을 μ°Ύμ•˜μŠ΅λ‹ˆκΉŒ?

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;
        });
    }
}

@G-yanpeng μ†”λ£¨μ…˜μ„ 더 μžμ„Ένžˆ μ„€λͺ…ν•΄ μ£Όμ‹œκ² μŠ΅λ‹ˆκΉŒ? ionic 4 angular8 버전에 λŒ€ν•΄μ„œλ„ λ™μΌν•œ μž‘μ—…μ„ μ‹œλ„ν•˜κ³  μžˆμŠ΅λ‹ˆλ‹€.

κ°μ‚¬ν•©λ‹ˆλ‹€!

같은 문제. λˆ„κ΅°κ°€ 해결책을 μ°Ύμ•˜μŠ΅λ‹ˆκΉŒ?

감사 ν•΄μš”!

@G-yanpeng μ†”λ£¨μ…˜μ„ 더 μžμ„Ένžˆ μ„€λͺ…ν•΄ μ£Όμ‹œκ² μŠ΅λ‹ˆκΉŒ?Ionic 4 angular8 λ²„μ „μ—μ„œλ„ λ™μΌν•œ μž‘μ—…μ„ μ‹œλ„ν•˜κ³  μžˆμŠ΅λ‹ˆλ‹€.

κ°μ‚¬ν•©λ‹ˆλ‹€!

Angular8μ—λŠ” html2canvasκ°€ μ œλŒ€λ‘œ μž‘λ™ν•˜μ§€ μ•ŠλŠ” μƒˆλ‘œμš΄ λ³€κ²½ 사항이 μžˆμŠ΅λ‹ˆλ‹€. 그런 λ‹€μŒ 이 μž„μ‹œ μ†”λ£¨μ…˜μ΄ μžˆμŠ΅λ‹ˆλ‹€. html2canvasκ°€ 각도 μ™ΈλΆ€μ—μ„œ μž‘λ™ν•˜λ„λ‘ ν•˜κ³ , κ°λ„λ‘œ μΈν•œ 영ν–₯을 ν”Όν•˜κΈ° μœ„ν•΄ 각도 ν•­λͺ©(μ•± 루트)κ³Ό λ™μΌν•œ λ ˆμ΄μ–΄μ— 이미지λ₯Ό μƒμ„±ν•˜λŠ” 데 ν•„μš”ν•œ dom을 λ³΅μ‚¬ν•©λ‹ˆλ‹€.

Angular8μ—λŠ” html2canvasκ°€ μ œλŒ€λ‘œ μž‘λ™ν•˜μ§€ μ•ŠλŠ” μƒˆλ‘œμš΄ λ³€κ²½ 사항이 μžˆμŠ΅λ‹ˆλ‹€.그런 λ‹€μŒ html2canvasκ°€ angular μ™ΈλΆ€μ—μ„œ μž‘λ™ν•˜λ„λ‘ ν•˜λŠ” μž„μ‹œ μ†”λ£¨μ…˜μ΄ μžˆμŠ΅λ‹ˆλ‹€. κ°λ„μ˜ 영ν–₯.
- ꡬ글 λ²ˆμ—­μœΌλ‘œ

image

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둜 λ³€κ²½ν–ˆμŠ΅λ‹ˆλ‹€. 이제 μ™„λ²½ν•˜κ²Œ μž‘λ™ν•©λ‹ˆλ‹€.

이 νŽ˜μ΄μ§€κ°€ 도움이 λ˜μ—ˆλ‚˜μš”?
0 / 5 - 0 λ“±κΈ‰