We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Html to PDF Conversion

Hi,


Attached is the screenshot of a html report. The bars are generated using the css styles on the client side. We are using Spring MVC and we need to convert this html report to pdf. The report can span over multiple pages.

Can you please guide how can we implement this using Aspose pdf.

Regards,

Hi Carol,


Thanks for your inquriy. You can convert HTML with CSS to PDF using Aspose.Pdf for Java. Please check following documentation link for sample code and details. However if you face any issue then please share your sample HTML+CSS files here, we will look into it and will guide you accordingly.


Best Regards,

Hi,


I am trying to set the page orientation to landscape for the generated pdf but it does not change. I am using the following code.

HtmlLoadOptions htmloptions = new HtmlLoadOptions(basePath);
htmloptions.getPageInfo().setWidth(PageSize.getA4().getWidth());
htmloptions.getPageInfo().setHeight(PageSize.getA4().getHeight());
htmloptions.getPageInfo().setLandscape(true);
/ Load HTML file
com.aspose.pdf.Document doc = new com.aspose.pdf.Document(basePath + “Report.html”, htmloptions);

Can you please guide what is the issue and why it is not setting the pdf as landscape.

The other issue I am facing is to retrieve the HTML from URL. I was thinking of he following approach.

URL pageUrl = new URL("");//tried with google url
URLConnection urlConnection = url.openConnection();
Document doc = new Document(urlConnection.getInputStream());

doc.save(“C:/test.pdf”);

But I get an error stating invalidformat.


Regards,

Hi Carol,


Thanks for sharing further details.

I am trying to set the page orientation to landscape for the generated pdf but it does not change

Please use following code snippet.

Java

HtmlLoadOptions htmloptions = new HtmlLoadOptions(dataDir);
htmloptions.getPageInfo().setWidth(PageSize.getA4().getWidth());
htmloptions.getPageInfo().setHeight(PageSize.getA4().getHeight());
OutputStream out = new java.io.ByteArrayOutputStream();
Document doc = new Document(dataDir + “input_test.htm”, htmloptions);
doc.save(out);
doc.getPages().get_Item(1).getPageInfo().setLandscape(true);
doc.processParagraphs();
// you can also use setRotate method to change the page orientation

// doc.getPages().get_Item(1).setRotate(Rotation.on270);

// doc.processParagraphs();
doc.save(dataDir +“LandScapeOutput.pdf”);
System.out.println(doc.getPages().get_Item(1).getPageInfo().isLandscape());

The other issue I am facing is to retrieve the HTML from URL

Please use following code snippet

JAVA

URL pageUrl = new URL("http://google.com");//tried with google url
URLConnection urlConnection = pageUrl.openConnection();
Document doc = new Document(urlConnection.getInputStream(), new HtmlLoadOptions(dataDir));
doc.save(dataDir + "testurl.pdf");

If you still face any issue or need further assistance, please feel free to contact us.

Best Regards,

Hi,


I tried the above code for applying the landscape but the generated pdf is always portrait. Also I tried adding the margins and i guess that is also an issue since it only applies default margins 72 and 90 points. The api says that the page is in landscape but looking at the pdf in print dialog, you can clearly see it is in portrait. When I print it on a page there is so much space on the left and right side which is not utilized.

Following is the complete code.

String basePath = “C:/Users/mathurrk/Desktop/”;
HtmlLoadOptions htmloptions = new HtmlLoadOptions(basePath);
htmloptions.getPageInfo().setWidth(PageSize.getA4().getWidth());
htmloptions.getPageInfo().setHeight(PageSize.getA4().getHeight());

OutputStream out = new java.io.ByteArrayOutputStream();
// Load HTML file
com.aspose.pdf.Document doc = new com.aspose.pdf.Document(basePath + “Report.html”, htmloptions);
doc.save(out);
doc.getPages().get_Item(2).getPageInfo().setLandscape(true);
doc.getPages().get_Item(3).getPageInfo().setLandscape(true);
doc.getPageInfo().getMargin().setBottom(400.0);
doc.getPageInfo().getMargin().setTop(400.0);
doc.getPageInfo().getMargin().setLeft(400.0);
doc.getPageInfo().getMargin().setRight(400.0);
doc.processParagraphs();

doc.save(“Location”);

Regards,

Hi Carol,


Thanks for sharing further details. I am looking into it, I will share my findings with you shortly.

Best Regards,


Hi Carol,


but the generated pdf is always portrait

I have tested the scenario and have managed to reproduce the problem that the generated PDF is portrait. For the sake of correction, I have logged a ticket PDFNET-42498 in our issue tracking system.

it only applies default margins 72 and 90 points

I have tested the scenario and have managed to reproduce the problem that generated PDF is not accepting the margin values. For the sake of correction, I have logged another ticket PDFNET-42499 in our issue tracking system.

We will further look into the details of these problem and will keep you updated within this forum thread on their status of resolution.

Moreover, you can also use setRotate method to change the page orientation. Please see following code snippet for reference

JAVA

HtmlLoadOptions htmloptions = new HtmlLoadOptions(dataDir);
htmloptions.getPageInfo().setWidth(PageSize.getA4().getWidth());
htmloptions.getPageInfo().setHeight(PageSize.getA4().getHeight());
OutputStream out = new java.io.ByteArrayOutputStream();
Document doc = new Document(dataDir + “input_test.htm”, htmloptions);
doc.getPages().get_Item(1).setRotate(Rotation.on270);
doc.processParagraphs();
doc.save(dataDir +“LandScapeOutput.pdf”);


You can use ContentsResizeParameters class to specify the parameters to be used to resize the pages(s) e.g. margins in percentage or units etc. The following code snippet shows how to resize the contents of some specific pages of the PDF file.

JAVA
HtmlLoadOptions htmloptions = new HtmlLoadOptions(dataDir);
OutputStream out = new java.io.ByteArrayOutputStream();
Document pdfDocument = new Document(dataDir + "input_test.htm", htmloptions);
//pdfDocument.getPages().get_Item(1).setRotate(Rotation.on270);
//pdfDocument.processParagraphs();
pdfDocument.save(out);

com.aspose.pdf.facades.PdfFileEditor pfe = new com.aspose.pdf.facades.PdfFileEditor();

IPdfFileEditor.ContentsResizeParameters params = PdfFileEditor.ContentsResizeParameters.pageResize(
com.aspose.pdf.PageSize.getA4().getHeight(), com.aspose.pdf.PageSize.getA4().getWidth());

for (int i = 1; i < pdfDocument.getPages().size() + 1; i++) {
params.setLeftMargin(IPdfFileEditor.ContentsResizeValue.units(200.0)); params.setRightMargin(IPdfFileEditor.ContentsResizeValue.units(200.0)); pfe.resizeContents(pdfDocument, new int[]{i}, params); }

pdfDocument.save(dataDir+"output.pdf");

If you still face any issue or need further assistance, please feel free to contact us.

We are sorry for the inconvenience.

Best Regards,