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

Free Support Forum - aspose.com

Converting HTML to PDF

Hi,


We have an URGENT requirement where we would like to convert HTML to PDF using Aspose PDF for Java. The requirements are provided below :

1. Ability to generate PDF from HTML String.
2. Set the Author, Document Title.
3. Set Page Headers, Footers.
4. Set Page Size, Margin and Rotation Angle.
5. Disable all hyperlinks in the generated pdf.
6. Add multiple URL’s : the location of Custom CSS.
7. Specify base URL to look up referred resources like images or CSS.

Output : a byte array of the generated PDF content.

We are currently using Pd4ML and would like to shift to Aspose PDF Java.

What have we tried so far :

1. We have made a POC with aspose.pdf.PDF class which seems to support most of what we need (I guess) except point 5, 6 & 7 above. Checked other forum posts and it seems we could put to use HTMLLoadOptions but not very clear as to how. Additionally, it seems we have to use com.aspose.pdf.Document to use the HTMLLoadOptions and I am not sure if it supports all the requirements we have.

2. Also checked couple of posts where the suggestion is to use Aspose Words Java.

What would we like to know :

1. Is there support in Aspose PDF Java for all the requirements provided ? If yes, can you please provide a code snippet for achieving them?

2. Which one is advisable Aspose PDF Java (or) Aspose Words Java for converting HTML to PDF ?

-Thanks

Hi,

Thanks for your inquiry.

tvkreddy92:
Hi,
We have an URGENT requirement where we would like to convert HTML to PDF using Aspose PDF for Java. The requirements are provided below :

  1. Ability to generate PDF from HTML String.
  2. Set the Author, Document Title.
  3. Set Page Headers, Footers.
  4. Set Page Size, Margin and Rotation Angle.
  5. Disable all hyperlinks in the generated pdf.
  6. Add multiple URL’s : the location of Custom CSS.
  7. Specify base URL to look up referred resources like images or CSS.
    Output : a byte array of the generated PDF content.

You can accomplish your requirements in two steps using Aspsoe.Pdf for Java. In first step convert HTML to PDF and in second step you can update PDF accordingly and save output to byte array.

You are right you can convert HTML to PDF using HtmlLoadOptions. You may either pass html file or stream(html string) to Document constructor for conversion. You can pass basepath of images/css to HtmlLoadOptions, treated as database for images/css. PageInfo property will be used for setting page size and margins.

After creating PDF document you can set rotation angle, header/footer, add hyperlinks using Aspose.Pdf for Java. Moreover, you can remove hyperlinks by searching linkAnnotations and set actions to null but I am afraid currently it is malfunctioning. We have already logged a ticket PDFNEWJAVA-34106 in our issue tracking system for rectification.

Moreover, in reference to Aspose.Words for Java prospective. I am moving thread to Aspose.Total and my colleague will update you about your requirements of HTML to PDF conversion.

Please feel free to contact us for any further assistance.

Best Regards,

Hi Tvk,

As far as Aspose.Words for Java is concerned, you can find all of your required features except rotation angle and disabling hyperlinks in the output PDF. You will have to use Aspose.Pdf for Java for these features.

If you want to use Aspose.Words for Java remaining features (other than two above mentioned features), you can download Aspose.Words for Java and check LoadOptions , HeaderFooter and PageSetup classes, and IResourceLoadingCallback interface in the doc folder of installation directory for detailed examples.

You can also check Work with Document Properties example to set title.

Best Regards,