2023-08-01 00:03:13 +01:00
package stirling.software.SPDF.controller.api ;
2023-09-02 00:05:50 +01:00
import java.awt.geom.AffineTransform ;
2023-09-02 19:12:08 +01:00
import java.io.ByteArrayInputStream ;
2023-08-01 00:03:13 +01:00
import java.io.ByteArrayOutputStream ;
2023-08-27 00:39:22 +01:00
import java.io.IOException ;
2023-09-02 19:12:08 +01:00
import java.io.InputStream ;
2023-08-01 00:03:13 +01:00
2023-09-02 00:05:50 +01:00
import org.apache.pdfbox.pdmodel.PDDocument ;
import org.apache.pdfbox.pdmodel.PDPage ;
import org.apache.pdfbox.pdmodel.common.PDRectangle ;
2023-09-02 19:12:08 +01:00
import org.apache.pdfbox.pdmodel.common.PDStream ;
2023-09-02 00:05:50 +01:00
import org.apache.pdfbox.pdmodel.graphics.form.PDFormXObject ;
2023-08-01 00:03:13 +01:00
import org.slf4j.Logger ;
import org.slf4j.LoggerFactory ;
import org.springframework.http.ResponseEntity ;
import org.springframework.web.bind.annotation.PostMapping ;
import org.springframework.web.bind.annotation.RequestPart ;
import org.springframework.web.bind.annotation.RestController ;
import org.springframework.web.multipart.MultipartFile ;
2023-09-02 20:21:55 +01:00
2023-08-27 00:39:22 +01:00
2023-08-01 00:03:13 +01:00
import io.swagger.v3.oas.annotations.Operation ;
import io.swagger.v3.oas.annotations.Parameter ;
import io.swagger.v3.oas.annotations.tags.Tag ;
import stirling.software.SPDF.utils.WebResponseUtils ;
2023-09-02 00:05:50 +01:00
import org.apache.pdfbox.pdmodel.PDPageContentStream ;
import org.apache.pdfbox.multipdf.LayerUtility ;
2023-08-01 00:03:13 +01:00
@RestController
@Tag ( name = "General" , description = "General APIs" )
public class ToSinglePageController {
private static final Logger logger = LoggerFactory . getLogger ( ToSinglePageController . class );
@PostMapping ( consumes = "multipart/form-data" , value = "/pdf-to-single-page" )
@Operation (
summary = "Convert a multi-page PDF into a single long page PDF" ,
description = "This endpoint converts a multi-page PDF document into a single paged PDF document. The width of the single page will be same as the input's width, but the height will be the sum of all the pages' heights. Input:PDF Output:PDF Type:SISO"
)
public ResponseEntity < byte []> pdfToSinglePage (
@RequestPart ( required = true , value = "fileInput" )
@Parameter ( description = "The input multi-page PDF file to be converted into a single page" , required = true )
MultipartFile file ) throws IOException {
2023-09-02 19:12:08 +01:00
// Load the source document
2023-09-02 00:05:50 +01:00
PDDocument sourceDocument = PDDocument . load ( file . getInputStream ());
2023-09-02 19:12:08 +01:00
// Calculate total height and max width
float totalHeight = 0 ;
float maxWidth = 0 ;
for ( PDPage page : sourceDocument . getPages ()) {
PDRectangle pageSize = page . getMediaBox ();
totalHeight += pageSize . getHeight ();
maxWidth = Math . max ( maxWidth , pageSize . getWidth ());
}
// Create new document and page with calculated dimensions
PDDocument newDocument = new PDDocument ();
PDPage newPage = new PDPage ( new PDRectangle ( maxWidth , totalHeight ));
newDocument . addPage ( newPage );
// Initialize the content stream of the new page
PDPageContentStream contentStream = new PDPageContentStream ( newDocument , newPage );
contentStream . close ();
LayerUtility layerUtility = new LayerUtility ( newDocument );
float yOffset = totalHeight ;
// For each page, copy its content to the new page at the correct offset
for ( PDPage page : sourceDocument . getPages ()) {
PDFormXObject form = layerUtility . importPageAsForm ( sourceDocument , sourceDocument . getPages (). indexOf ( page ));
AffineTransform af = AffineTransform . getTranslateInstance ( 0 , yOffset - page . getMediaBox (). getHeight ());
layerUtility . wrapInSaveRestore ( newPage );
String defaultLayerName = "Layer" + sourceDocument . getPages (). indexOf ( page );
layerUtility . appendFormAsLayer ( newPage , form , af , defaultLayerName );
yOffset -= page . getMediaBox (). getHeight ();
}
ByteArrayOutputStream baos = new ByteArrayOutputStream ();
newDocument . save ( baos );
newDocument . close ();
sourceDocument . close ();
byte [] result = baos . toByteArray ();
return WebResponseUtils . bytesToWebResponse ( result , file . getOriginalFilename (). replaceFirst ( "[.][^.]+$" , "" ) + "_singlePage.pdf" );
2023-08-01 00:03:13 +01:00
}
}