37 lines
1.5 KiB
Java
37 lines
1.5 KiB
Java
package org.opencdmp.filetransformer.docx.service.pdf;
|
|
|
|
import org.springframework.core.io.ByteArrayResource;
|
|
import org.springframework.http.MediaType;
|
|
import org.springframework.http.client.MultipartBodyBuilder;
|
|
import org.springframework.stereotype.Component;
|
|
import org.springframework.web.reactive.function.BodyInserters;
|
|
import org.springframework.web.reactive.function.client.WebClient;
|
|
|
|
import java.util.UUID;
|
|
|
|
@Component
|
|
public class PdfServiceImpl implements PdfService {
|
|
|
|
private final PdfServiceProperties pdfServiceProperties;
|
|
|
|
public PdfServiceImpl(PdfServiceProperties pdfServiceProperties) {
|
|
this.pdfServiceProperties = pdfServiceProperties;
|
|
}
|
|
|
|
@Override
|
|
public byte[] convertToPDF(byte[] file) {
|
|
WebClient webClient = WebClient.builder().baseUrl(pdfServiceProperties.getUrl()).build();
|
|
MultipartBodyBuilder builder = new MultipartBodyBuilder();
|
|
builder.part("files", new ByteArrayResource(file)).filename(UUID.randomUUID() + ".docx");
|
|
|
|
return webClient.post().uri("forms/libreoffice/convert")
|
|
.headers(httpHeaders -> {
|
|
httpHeaders.setContentType(MediaType.MULTIPART_FORM_DATA);
|
|
httpHeaders.add("Content-disposition", "attachment; filename=" + UUID.randomUUID() + ".pdf");
|
|
httpHeaders.add("Content-type", "application/pdf");
|
|
})
|
|
.body(BodyInserters.fromMultipartData(builder.build()))
|
|
.retrieve().bodyToMono(byte[].class).block();
|
|
}
|
|
}
|