Created
September 18, 2024 09:36
-
-
Save smadil997/1d76e8628f3a71b2b676778acfd82896 to your computer and use it in GitHub Desktop.
Create controller which we will called from postman
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.puppeteer.pdf.controller; | |
import com.puppeteer.pdf.serivce.CreatePDFService; | |
import jakarta.servlet.http.HttpServletResponse; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.core.io.ByteArrayResource; | |
import org.springframework.http.HttpHeaders; | |
import org.springframework.http.HttpStatus; | |
import org.springframework.http.MediaType; | |
import org.springframework.http.ResponseEntity; | |
import org.springframework.web.bind.annotation.*; | |
import java.io.File; | |
import java.io.IOException; | |
import java.nio.file.Files; | |
import java.nio.file.Path; | |
import java.nio.file.Paths; | |
@RestController | |
@RequestMapping("/pdf") | |
public class CreatePDFController { | |
@Autowired | |
private CreatePDFService pdfService; | |
@GetMapping("/generate/{name}") | |
public ResponseEntity<?> createPDF(@PathVariable String name) throws IOException { | |
File file =pdfService.createPDF(name); | |
Path path = Paths.get(file.getAbsolutePath()); | |
ByteArrayResource resource=new ByteArrayResource(Files.readAllBytes(path)); | |
HttpHeaders header = new HttpHeaders(); | |
header.add(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=img.pdf"); | |
header.add("Cache-Control", "no-cache, no-store, must-revalidate"); | |
header.add("Pragma", "no-cache"); | |
header.add("Expires", "0"); | |
return ResponseEntity.ok() | |
.headers(header) | |
.contentLength(file.length()) | |
.contentType(MediaType.parseMediaType("application/octet-stream")) | |
.body(resource); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment