Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

.PDF_DataFile, Cannot decode line "1" as it contains invalid PNG Base64 data #19

Open
Aron0025 opened this issue Sep 13, 2023 · 5 comments

Comments

@Aron0025
Copy link

image
I had to change the 'let doc = "" ' into something else like ' let adoc = "" ' in the method 1 script to even get it to work in the console, otherwise it showed ' Identifier 'doc' has already been declared '.

@stepkrep
Copy link

stepkrep commented Oct 2, 2023

Try this code, then use GeneratePDF.cmd

https://pastebin.com/qasrDhhJ

@Aron0025
Copy link
Author

Aron0025 commented Oct 4, 2023

Try this code, then use GeneratePDF.cmd

https://pastebin.com/qasrDhhJ

Thank you!

@taile431909
Copy link

Hi, the pastebin doesn't work anymore. Is there any other workarounds for this?

@stepkrep
Copy link

stepkrep commented Nov 29, 2023

Hi, the pastebin doesn't work anymore. Is there any other workarounds for this?

let pdfDocumentName = "Document";

function generatePDF_DataFile() {
  let doc = ""; // Dichiarazione locale di doc all'interno della funzione

  let imgTags = document.getElementsByTagName("img");
  let checkURLString = "blob:https://drive.google.com/";
  let validImgTagCounter = 0;

  for (let i = 0; i < imgTags.length; i++) {
    if (imgTags[i].src.substring(0, checkURLString.length) === checkURLString) {
      validImgTagCounter = validImgTagCounter + 1;

      let img = imgTags[i];

      let canvas = document.createElement('canvas');
      let context = canvas.getContext("2d");
      canvas.width = img.naturalWidth;
      canvas.height = img.naturalHeight;

      context.drawImage(img, 0, 0, img.naturalWidth, img.naturalHeight);
      let imgDataURL = canvas.toDataURL();

      if (doc === "") {
        doc = imgDataURL;
      } else {
        doc = doc + "\n" + imgDataURL;
      }
    }
  }

  let anchorElement = document.createElement("a");
  let file = new Blob([doc], { type: 'text/plain' });

  url = URL.createObjectURL(file);
  anchorElement.href = url;
  anchorElement.download = pdfDocumentName + ".PDF_DataFile";
  document.body.appendChild(anchorElement);
  anchorElement.click();
}

let allElements = document.querySelectorAll("*");
let chosenElement;
let heightOfScrollableElement = 0;

for (let i = 0; i < allElements.length; i++) {
  if (allElements[i].scrollHeight >= allElements[i].clientHeight) {
    if (heightOfScrollableElement < allElements[i].scrollHeight) {
      chosenElement = allElements[i];
      heightOfScrollableElement = allElements[i].scrollHeight;
    }
  }
}

if (chosenElement.scrollHeight > chosenElement.clientHeight) {
  console.log("Auto Scroll");

  let scrollDistance = Math.round(chosenElement.clientHeight / 2);

  let loopCounter = 0;
  function myLoop(remainingHeightToScroll, scrollToLocation) {
    loopCounter = loopCounter + 1;
    console.log(loopCounter);

    setTimeout(function() {
      if (remainingHeightToScroll === 0) {
        scrollToLocation = scrollDistance;
        chosenElement.scrollTo(0, scrollToLocation);
        remainingHeightToScroll = chosenElement.scrollHeight - scrollDistance;
      } else {
        scrollToLocation = scrollToLocation + scrollDistance;
        chosenElement.scrollTo(0, scrollToLocation);
        remainingHeightToScroll = remainingHeightToScroll - scrollDistance;
      }

      if (remainingHeightToScroll >= chosenElement.clientHeight) {
        myLoop(remainingHeightToScroll, scrollToLocation);
      } else {
        setTimeout(function() {
          generatePDF_DataFile();
        }, 1500);
      }
    }, 400);
  }

  myLoop(0, 0);
} else {
  console.log("No Scroll");
  setTimeout(function() {
    generatePDF_DataFile();
  }, 1500);
}

This doesn't work?

@taile431909
Copy link

Yooo, thanks man. The code works. It was just that the pastebin link didn't work. Thank you again man, you doin' god's work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants