Spring Boot 백엔드

열기 · 페이지 이미지 · 저장 세 엔드포인트를 Spring Boot 컨트롤러로 처리합니다.

1.GET /document

PDF를 열어 문서 모델(JSON)과 페이지 이미지(PNG)를 반환합니다.

JAVADocumentController.java
@SpringBootApplication
public class CoreDocsApplication {
  public static void main(String[] args) { SpringApplication.run(CoreDocsApplication.class, args); }
}

@RestController
@RequestMapping("/document")
class DocumentController {
  static final Path PDF = Paths.get("C:/data/pdf"), CACHE = Paths.get("C:/data/cache");

  @GetMapping("/{name}")   // 열기 — return 하면 Jackson 자동 직렬화
  Document open(@PathVariable String name) throws IOException {
    return Document.loadFromPDF(PDF.resolve(name), CACHE.resolve(name));
  }

  @GetMapping(value = "/{name}/page/{i}/render", produces = MediaType.IMAGE_PNG_VALUE)   // 페이지 이미지
  byte[] render(@PathVariable String name, @PathVariable int i,
                @RequestParam(defaultValue = "1") float scale) throws IOException {
    return Files.readAllBytes(Document.getPageRenderImagePath(CACHE.resolve(name), i, scale));
  }
}
• 페이지 이미지

PNG는 loadFromPDF 시점에 cache에 만들어지고, getPageRenderImagePath로 그 경로만 얻어 반환합니다.

2.POST /save

뷰어는 저장 시 document와 imageFileList를 multipart로 전송합니다. 자세한 내용은 문서 저장을 참고하세요.

JAVA저장 엔드포인트
@PostMapping("/document/{name}/save/{saveName}")
Document save(@PathVariable String name, @PathVariable String saveName,
    @RequestPart("document") Document document,
    @RequestPart(name = "imageFileList", required = false) MultipartFile[] imageFileList) throws IOException {
  document.applyToPDF(PDF.resolve(name), toMap(imageFileList));   // 이미지 반영
  document.saveToPDF(PDF.resolve(saveName), CACHE.resolve(saveName));
  return document;
}

3.실행

CoreDocs JAR와 필요한 외부 라이브러리를 선언합니다.

GROOVYbuild.gradle
dependencies {
  implementation files('libs/coredocs-pdf-core-java-1.0.0-all.jar')
  implementation 'org.springframework.boot:spring-boot-starter-web'
  implementation 'org.apache.pdfbox:pdfbox:3.0.2'
}
BASH실행
./gradlew bootRun   # 포트는 application.yml 의 server.port 로 지정

뷰어의 fetcher가 이 엔드포인트를 호출하도록 URL을 맞추어 작성합니다.