0

I am trying to convert html to pdf using openhtmltopdf. I'm using mave openhtmltopdf.

Then I write the Main class (example below), but the problem is that I need the landscape orientation of the page and it was possible to adjust the font (now everything is moving out), what should I do for this?

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.file.FileSystems;
import org.jsoup.Jsoup;
import org.jsoup.helper.W3CDom;
import org.jsoup.nodes.Document;
import com.openhtmltopdf.pdfboxout.PdfRendererBuilder;

public class Main {
    public static void main(String[] args) {
        try {
            // HTML file - Input
            File inputHTML = new File("D:\\Загрузки от 13.05\\htmltopdf\\4874063.html");
            // Converted PDF file - Output
            String outputPdf = "D:\\Загрузки от 13.05\\htmltopdf\\Test.pdf";
            Main htmlToPdf = new Main();
            //create well formed HTML
            org.w3c.dom.Document doc = htmlToPdf.createWellFormedHtml(inputHTML);
            System.out.println("Starting conversion to PDF...");
            htmlToPdf.xhtmlToPdf(doc, outputPdf);
        } catch (IOException e) {
            System.out.println("Error while converting HTML to PDF " + e.getMessage());
            e.printStackTrace();
        }
    }

    // Creating well formed document
    private org.w3c.dom.Document createWellFormedHtml(File inputHTML) throws IOException {
        Document document = Jsoup.parse(inputHTML, "UTF-8");
        document.outputSettings().syntax(Document.OutputSettings.Syntax.xml);
        System.out.println("HTML parsing done...");
        return new W3CDom().fromJsoup(document);
    }

    private void xhtmlToPdf(org.w3c.dom.Document doc, String outputPdf) throws IOException {
        // base URI to resolve future resources
        String baseUri = FileSystems.getDefault()
                .getPath("F:/", "Anshu/NetJs/Programs/", "src/main/resources/template")
                .toUri()
                .toString();
        OutputStream os = new FileOutputStream(outputPdf);
        PdfRendererBuilder builder = new PdfRendererBuilder();
        builder.withUri(outputPdf);
        builder.toStream(os);
        // add external font
        //builder.useFont(new File(getClass().getClassLoader().getResource("fonts/PRISTINA.ttf").getFile()), "PRISTINA");
        builder.withW3cDocument(doc, baseUri);
        builder.run();
        System.out.println("PDF creation completed");
        os.close();
    }
}```
Omid Nazifi
  • 5,235
  • 8
  • 30
  • 56
Ekz0
  • 63
  • 1
  • 8
  • Do I need to find this html somewhere in the project files and edit it? – Ekz0 Jun 02 '21 at 14:02
  • It turns out that in order to get the correct html I will not be able to change something in the code? Change orientation to landscape, for example. – Ekz0 Jun 02 '21 at 14:26
  • html mean which I submit to the input? – Ekz0 Jun 02 '21 at 15:03
  • If I understand correctly, I need to add functions in the source code on the gita that will edit the incoming html and add the necessary parameters to the "head"? Or set the required parameters for the pdf in the source code? – Ekz0 Jun 04 '21 at 12:31

0 Answers0