1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Anuncie Aqui
    Anuncie aqui você Também: fdantas@4each.com.br

How to Handle CORS Issues with html2Canvas and AWS S3 Images?

Discussão em 'Angular' iniciado por PASHA, Novembro 12, 2024.

  1. PASHA

    PASHA Guest

    I'm facing a CORS policy issue when trying to print a PDF using Angular with html2canvas.

    Access to image at 'https://{bucket-name}.s3.amazonaws.com/' from origin 'https://localhost:4200' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

    Though I've seen similar questions before, I still can't get it to work. I have a div containing images loaded from an AWS S3 bucket

    SendHtmlForPdf() {
    const data = document.getElementById('testdivid');

    if (!data) {
    console.error("Element with id 'testdivid' not found!");
    return;
    }

    const originalMaxHeight = data.style.maxHeight;
    data.style.maxHeight = 'none';

    html2canvas(data, {
    scale: 2,
    logging: true,
    allowTaint: false,
    useCORS: true,
    })
    .then((canvas) => {
    const contentDataURL = canvas.toDataURL('image/png', 1.0);

    const pdf = new jsPDF('p', 'in', 'letter');
    const pdfWidth = 8.5;
    const imgHeight = (canvas.height * pdfWidth) / canvas.width; // 11
    const position = 0;

    pdf.addImage(contentDataURL, 'PNG', 0, position, pdfWidth, 11);
    // pdf.save('Filename.pdf');
    const pdfUrl = pdf.output('bloburl');
    const printWindow = window.open(pdfUrl, '_blank');
    if (printWindow) {
    printWindow.print();
    }
    data.style.maxHeight = originalMaxHeight;
    })
    .catch((error) => {
    console.error('Error generating PDF:', error);

    data.style.maxHeight = originalMaxHeight;
    });


    }

    [
    {
    "AllowedHeaders": [
    "*"
    ],
    "AllowedMethods": [
    "GET"
    "PUT",
    "POST",
    "DELETE"
    ],
    "AllowedOrigins": [
    "*"
    ],
    "ExposeHeaders": [
    "x-amz-server-side-encryption",
    "x-amz-request-id",
    "x-amz-id-2"
    ],
    "MaxAgeSeconds": 3000
    }


    ]

    Continue reading...

Compartilhe esta Página