I'm using the MPL/LGPL version of the iText library (the one released in July 2009) to download pdfs through a web application. My issue is that the GSP that is being rendered on the pdf has a landscape layout. Currently I have this code:
public void mergeMultiplePdfFiles(List<String> fileNames, OutputStream os, String fileDirectory, boolean isLandscape = false) {
FileInputStream is
PdfReader pdfReader
PdfWriter pdfWriter
com.lowagie.text.Document document
try {
document = new com.lowagie.text.Document(isLandscape ? PageSize.LETTER.rotate() : PageSize.LETTER)
pdfWriter = PdfWriter.getInstance(document, os)
document.open()
PdfImportedPage page
PdfContentByte cb = pdfWriter.getDirectContent() // Holds the PDF
fileNames.each { fileName ->
def filePath = fileDirectory + fileName + ".pdf"
is = new FileInputStream(filePath)
pdfReader = new PdfReader(is)
for(int i = 1; i <= pdfReader.getNumberOfPages(); i++) {
document.newPage()
page = pdfWriter.getImportedPage(pdfReader, i)
//cb.addTemplate(page, 0.0, -1f, 1f, 0.0, 0.0, pdfReader.getPageSizeWithRotation(i).height);
cb.addTemplate(page, 0, 0);
}
is.close()
pdfReader.close();
File file = new File(filePath)
file.delete()
}
}
catch (Exception e) {
log.error("ERROR Generating a PDF.")
//e.printStackTrace()
throw e
}
finally {
if (document.isOpen())
document.close()
try {
if (os != null) os.close()
if (is != null) is.close()
if (pdfReader != null) pdfReader.close()
if (pdfWriter != null) pdfWriter.close()
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
}
This code creates a pdf that is rendered properly but the page is in portrait. So, the user has to rotate the pdf to landscape to read the document. I want to have the pdf rotated to landscape before downloading the file.
I've attempted many solution such as:
document = new com.lowagie.text.Document(PageSize.LETTER.rotate())
or alternatively,
document.setPageSize(PageSize.LETTER.rotate())
these two solution cause the pdf to be set to landscape but the text is cut off in the portrait view. Also,
and
rot = pdfReader.getPageRotation(i);
pageDict = pdfReader.getPageN(i);
pageDict.put(PdfName.ROTATE, new PdfNumber(rot + 90));
rot = pdfReader.getPageRotation(i);
None of these suggested solutions work.