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.
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.
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
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.
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);
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-42499in 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();