I am trying to find a Java library that can convert from a HTML to a PDF using the CSS printing styles.
I need that this library be free for commercial use. Not like itext that you need to pay or use AGPL license showing all your code.
Software Recommendations Stack Exchange is a question and answer site for people seeking specific software recommendations. It only takes a minute to sign up.
Sign up to join this communityYou have a few options:
Finally, the library choose was Flying Saucer that uses the capacities from an old open source version in itext and allow us to use not all CSS printing styles but at least the important ones.
https://github.com/flyingsaucerproject/flyingsaucer
Flying saucer accepts the CSS 2.1 Specification. You can get more info about it in link and check the CSS specification in internet (I have not privileges to put more than 2 links)
You can use Cloudmersive Convert API which is free for commercial use up to 1K conversions/month. Here is how you can call it in Java:
// Import classes:
//import com.cloudmersive.client.invoker.ApiClient;
//import com.cloudmersive.client.invoker.ApiException;
//import com.cloudmersive.client.invoker.Configuration;
//import com.cloudmersive.client.invoker.auth.*;
//import com.cloudmersive.client.ConvertDocumentApi;
ApiClient defaultClient = Configuration.getDefaultApiClient();
// Configure API key authorization: Apikey
ApiKeyAuth Apikey = (ApiKeyAuth) defaultClient.getAuthentication("Apikey");
Apikey.setApiKey("YOUR API KEY");
// Uncomment the following line to set a prefix for the API key, e.g. "Token" (defaults to null)
//Apikey.setApiKeyPrefix("Token");
ConvertDocumentApi apiInstance = new ConvertDocumentApi();
File inputFile = new File("/path/to/inputfile"); // File | Input file to perform the operation on.
Boolean includeBackgroundGraphics = true; // Boolean | Optional: Set to true to include background graphics in the PDF, or false to not include. Default is true.
Integer scaleFactor = 56; // Integer | Optional: Set to 100 to scale at 100%, set to 50% to scale down to 50% scale, set to 200% to scale up to 200% scale, etc. Default is 100%. Maximum is 1000%.
try {
byte[] result = apiInstance.convertDocumentHtmlToPdf(inputFile, includeBackgroundGraphics, scaleFactor);
System.out.println(result);
} catch (ApiException e) {
System.err.println("Exception when calling ConvertDocumentApi#convertDocumentHtmlToPdf");
e.printStackTrace();
}
I know this is a bit old topic, but for future reference and future coders, I'm suggesting to use https://pdfmyurl.com which I personally use.
It does have a code example for .NET / C#, Java, JavaScript, PHP, Perl, Ruby, Python and VB.net.
Aside from the programmer-friendly platform, It does have a well-documented tutorial that can be easily followed.
Not a Java library, but you can execute Chrome in headless mode (no GUI, just like Command Line), and order it to create a PDF or PNG file of the page (URL) you want.