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

Free Support Forum - aspose.com

How can I convert Html String to Pdf?

Hi.
I bought a PDF license a few month ago.
I want to make the PDF using Html String.
But all of the sample code use Html File.

I have a long Html String(Not File).
I think html String convert to html file then convert to PDF is not good performance
So, can I get solution?
If the solution isn’t exist, can you recommend a way or a library package?
(I use Aspose.PDF Java. jdk16)

Thank you for reading

@emrokr

Thanks for contacting support.

Please use following code snippet to add HTML string inside PDF without usage of any HTML file.

Document doc = new Document();
Page page = doc.getPages().add();
HtmlFragment htmlFragment = new HtmlFragment("<h1>HTML String</h1>");
page.getParagraphs().add(htmlFragment);
doc.save(dataDir + "HTMLStringUsingDOM.pdf");

In case you need further assistance, please feel free to let us know.

Hi team , I am also having same requirement but instead of saving the pdf in location , our system should display the dialog box and should be downloaded in browser or end user system…nput will be “HTML” input and should be converted to PDF on the fly which will be downloaded in browser.

@vanapandi

Thanks for contacting support.

You may please follow the instructions given in following article to send the generated PDF to browser for download:

Hello asad,

I am using the same code but not working could you please let me know the assemblies which I need to include for Document, Page and HtmlFragment class?

Also the link shared below is not working. I have the same scenario.

Regards
Sudrashya

@Sudrashya

You only need to install or add reference to Aspose.PDF for .NET/Java as per your development environment in order to use these classes. Furthermore, you can check the code for sending PDF to Browser for Download in this forum thread.

@asad.ali
Aspose.PDF.Document instance does not contain the definition of getPages().

@Sudrashya

The getPages() method is in Aspose.PDF for Java API. For .NET, you can access it as Aspose.Pdf.Document.Pages. Please share the complete Java code snippet if you are facing issues while using it in .NET. We will convert it for you and share it with you.