인터넷에 보기 전용(view-only) 구글 드라이브 pdf를 다운로드하는 방법을 검색하면 많이 나온다.
개발자 도구의 콘솔 창에서 명령어를 입력하여 pdf를 한장한장 이미지로 저장하고, 그 이미지를 다시 pdf로 저장하는 간단한 방식이다.
아래 글이 원글인 모양인데, 그 외에도 많은 사람들이 개인 블로그나 깃헙에 공유한 걸 확인할 수 있다.
https://codingcat.codes/2019/01/09/download-view-protected-pdf-google-drive-js-code/
How to download view only protected PDF from Google Drive (JS code) - Coding Cat
I’ve had some document that I couldn’t read on Android phone or iPad showing error because of read only mode, that the mobile devices could not handle. I wanted to read it on other devices than the PC, so I wrote a little ‘hack’ in JS. Note 1: It
codingcat.codes
기존 방식
위 글을 간단히 요약하고 번역하자면, 다음과 같다.
1. 구글 크롬에서 보기 전용 pdf를 연다.
2. 모든 페이지를 인식할 수 있도록 천천히 스크롤하면서 끝까지 페이지가 생성됨을 확인한다.
3. F12를 눌러 개발자도구를 연다.
4. 콘솔 창에 다음 명령어를 복붙한다.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
let jspdf = document.createElement("script");
jspdf.onload = function () {
let pdf = new jsPDF();
let elements = document.getElementsByTagName("img");
for (let i in elements) {
let img = elements[i];
console.log("add img ", img);
if (!/^blob:/.test(img.src)) {
console.log("invalid src");
continue;
}
let can = document.createElement('canvas');
let con = can.getContext("2d");
can.width = img.width;
can.height = img.height;
con.drawImage(img, 0, 0, img.width, img.height);
let imgData = can.toDataURL("image/jpeg", 1.0);
pdf.addImage(imgData, 'JPEG', 0, 0);
pdf.addPage();
}
pdf.save("download.pdf");
};
jspdf.src = 'https'+'://'+'cdnjs'+'.cloudflare'+'.com/ajax/libs/jspdf/1.5.3/jspdf.debug.js';
document.body.appendChild(jspdf);
|
TrustedScriptUrl 에러
그러나 작년(2023년) 언젠가부터, 위에 나온 코드를 크롬에서 실행하면 TrustedScriptUrl 에러가 발생한다.
이 오류를 호소하는 사람들이 많은데, 해결 방법은 간단하다.
>> 바로 크롬이 아니라 파이어폭스(firefox)를 통해서 위 코드를 시행하는 것이다!

파이어폭스가 없다면 다운로드하자. 생각보다 그렇게 오래걸리지 않는다.
그러면 위 문제를 바로 해결할 수 있다. (2024년 4월 기준 정상 작동 확인)
'컴퓨터 TIP' 카테고리의 다른 글
WSL(Ubuntu)에 MariaDB 설치 오류 해결 (ERROR 2002 (HY000)) (0) | 2025.03.03 |
---|---|
카카오톡에는 글자 수 제한이 있을까? (2) | 2024.02.09 |