빠른 시작
1.프로젝트 구조
TEXT디렉터리
projectName/
├─ build.gradle
├─ pdf/devsite.pdf # 열 PDF
├─ cache/ # 이미지 cache (자동 생성)
└─ src/main/
├─ java/App.java # Spring Boot 앱 + 문서 API
└─ resources/
├─ application.yml # server.port: 8102
└─ static/ # 프론트 — Spring Boot 가 자동 서빙
├─ index.html
├─ style.css
├─ js/app.js
└─ lib/
├─ coredocs-pdf-core-1.0.0.css
└─ coredocs-pdf-core-1.0.0.jsCoreDocs JAR와 의존성은 build.gradle에 선언합니다. 준비물은 설치를 참고하세요.
2.예제
실행 예제: 뷰어 미리보기준비 중…
HTMLpublic/index.html
<!doctype html>
<html lang="ko">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" href="./style.css" />
<link rel="stylesheet" href="/lib/coredocs-pdf-core-1.0.0.css" />
</head>
<body>
<div id="viewer"></div>
<script type="module" src="./js/app.js"></script>
</body>
</html>CSSpublic/style.css
* {
box-sizing: border-box;
padding: 0;
margin: 0;
}
#viewer {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
width: 50%;
height: 85vh;
}JSpublic/js/app.js
import { createPDFViewer } from '/lib/coredocs-pdf-core-1.0.0.js';
const fileName = 'devsite.pdf';
const viewer = createPDFViewer('#viewer', {
licenseKey: '발급받은-라이선스-키',
fetcher: {
open: async () => {
const res = await fetch('/document/' + fileName);
return await res.json();
},
pageRender: async ({ page, renderScale = 2 }) => {
const res = await fetch('/document/' + fileName + '/page/' + page.pageIndex + '/render?scale=' + renderScale);
return await res.blob();
},
},
});
await viewer.open();JAVAApp.java
import kr.co.inogard.coredocs.pdf.Document;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import java.io.IOException;
import java.nio.file.*;
@SpringBootApplication
public class App {
public static void main(String[] args) {
Document.setLicenseKey("발급받은-라이선스-키");
SpringApplication.run(App.class, args);
}
}
// 문서 API (열기 / 페이지 이미지) — 정적 파일은 Spring Boot 가 자동 서빙
@RestController
@RequestMapping("/document")
class DocumentController {
// 열기: GET /document/{name}
@GetMapping("/{name}")
Document open(@PathVariable String name) throws IOException {
return Document.loadFromPDF(Paths.get("pdf/" + name));
}
// 페이지 이미지: GET /document/{name}/page/{i}/render
@GetMapping(value = "/{name}/page/{i}/render", produces = MediaType.IMAGE_PNG_VALUE)
byte[] render(@PathVariable String name, @PathVariable int i) throws IOException {
Document.loadFromPDF(Paths.get("pdf/" + name), Paths.get("cache/" + name));
Path png = Document.getPageRenderImagePath(Paths.get("cache/" + name), i, 2.0f);
return Files.readAllBytes(png);
}
}• 라이선스 키
발급받은-라이선스-키 자리에 실제 키를 넣습니다. 발급은 라이선스 키를 참고하세요.
3.실행
BASH실행
# :8102 로 뜬다 (application.yml 의 server.port)
./gradlew bootRun