11

I am converting html to pdf using iText7 with method convertToPdf(). PDF is getting generated properly but Landscape mode is not working.

Can some one tell how to get Landscape mode?

import com.itextpdf.html2pdf.ConverterProperties;
import com.itextpdf.html2pdf.HtmlConverter;
import com.itextpdf.styledxmlparser.css.media.MediaDeviceDescription;
import com.itextpdf.styledxmlparser.css.media.MediaType;

import java.io.File;
import java.io.IOException;

import static com.itextpdf.html2pdf.css.CssConstants.LANDSCAPE;

public class htmlToPDF {

    public static void main(String args[]) throws IOException {

        ConverterProperties properties = new ConverterProperties();

        MediaDeviceDescription med = new MediaDeviceDescription(MediaType.ALL);
        med.setOrientation(LANDSCAPE);
        properties.setMediaDeviceDescription(med);

        HtmlConverter.convertToPdf(new File("D:\\test.html"), new File("D:\\test.pdf"),properties);
    }
}
Max
  • 915
  • 10
  • 28
NeedToLearn
  • 147
  • 1
  • 2
  • 11

2 Answers2

21

Please just use a converter method that takes PdfDocument as a parameter. For example, the next one : convertToPdf(InputStream htmlStream, PdfDocument pdfDocument, ConverterProperties converterProperties)

Now the only thing you need is to set the page size to the document before converting the html file.

    PdfDocument pdfDocument = new PdfDocument(new PdfWriter(new File(sourcePath)));
    pdfDocument.setDefaultPageSize(PageSize.A4.rotate());
    HtmlConverter.convertToPdf(new FileInputStream(destPath), pdfDocument, new ConverterProperties());
Uladzimir Asipchuk
  • 2,368
  • 1
  • 9
  • 19
  • 2
    Thank you, pdfDocument.setDefaultPageSize(PageSize.A4.rotate()) was exactly what I was looking for because I want to rotate only some pages of my document – Rapwnzel Mar 12 '19 at 11:27
  • 1
    Please add below line of code which is missing and creates confusion. `var props = new ConverterProperties ();` – Rajeev Kumar Aug 03 '22 at 15:29
  • @Rapwnezel: and how do you rotate only some pages ? – ittradco Aug 10 '23 at 12:56
2

You can use PageOrientationsEventHandler to handle orientation in your document like -

PdfDocument pdfDoc = new PdfDocument(new PdfWriter(DEST));
PageOrientationsEventHandler eventHandler = new PageOrientationsEventHandler();
pdfDoc.addEventHandler(PdfDocumentEvent.START_PAGE, eventHandler);
Document doc = new Document(pdfDoc);
doc.add(new Paragraph("A simple page in portrait orientation"));
eventHandler.setOrientation(LANDSCAPE);

check it in more detail here.

Max
  • 915
  • 10
  • 28
Sai prateek
  • 11,842
  • 9
  • 51
  • 66
  • 1
    But how can I pass html file as input? I am converting a html file to pdf. – NeedToLearn Jan 24 '19 at 13:32
  • that's a good suggestion if someone wants his document to have pages of different orientation. However if one just want to have some certain orientation set on the whole document, there is no need in handling START_PAGE event – Uladzimir Asipchuk Jan 24 '19 at 13:56