Convert HTML String to PDF

Hi,


I am using Aspose 4.5 for java for one of our project.

1) I have a requirement to convert HTML string to PDF .Again this generated PDF from Html String should be merged and should be added at the end of PDF pages as a seperate page.

Do you have any API to satisfy this requirement.

2) I have a code from aspose PDF which generates PDF from an HTML file location (not from HTML String) using document object.But this shows error when tag appears in HTML
com.aspose.ms.System.au: Object reference not set to an instance of an object.
at com.aspose.ms.System.bi.b(Unknown Source)
at aspose.pdf.html.pdfadapters.PdfFlowDocumentBuilder.a(Unknown Source)
at aspose.pdf.html.pdfadapters.PdfFlowDocumentBuilder.visitImg(Unknown Source)


Any help regarding these issues would be highly appreciated .

Please give an update.We are migrating itext to Aspose.This is a showstopper for us

Hi Basil,

Thanks for your interest in our API’s.

In order to convert HTML string to PDF format, please try using the following code snippet.

[Java]

```java
aspose.pdf.Pdf pdf = new aspose.pdf.Pdf();
aspose.pdf.Section sec = pdf.getSections().add();

String sample = "This is a test *for **HTML** support** ***" +
                " ~~in Text paragraph.~~ ";

aspose.pdf.Text text1 = new aspose.pdf.Text(sample);
text1.isHtmlTagSupported(true);

sec.getParagraphs().add(text1);

pdf.save("c:/pdftest/HTMLString_to_PDF.pdf");
```

In case you need to convert HTML file to PDF format, please try using the code snippet specified below.

[Java]

```java
// Instantiate HTML load options object
com.aspose.pdf.HtmlLoadOptions html = new com.aspose.pdf.HtmlLoadOptions();

// Specify to use the new HTML to PDF conversion engine of DOM approach
html.setUseNewConversionEngine(true);

// Load HTML file contents
com.aspose.pdf.Document doc = new com.aspose.pdf.Document("c:/pdftest/sample (1).html", html);

// Save the output in PDF format
doc.save("c:/pdftest/TestDoc.pdf");
```

Once the HTML String/File is converted to PDF format, you can concatenate the newly created PDF file to existing PDF documents. For further details, please visit Concatenate PDF Files.

In case of any further queries, please feel free to contact me.

Hi ,

Thank you for the reply.
My requirement was to create PDF from HTML string and concatenate with an existing PDF object in com.aspose.pdf.Document. I do not want to load the PDF file from a location in class path as per Merge PDF programmatically|Aspose.PDF for Java

Please find the code done for my requirement .Here I am trying to convert aspose.pdf.Pdf pdf via new com.aspose.pdf.Document(pdf);.The pages in first document are showing blank and only last page is getting added properly.

Please go though the code .You can save the file as TestAsposeV1.java and run the code locally.

Please help…

import java.io.IOException;
import java.util.Locale;

import com.aspose.pdf.Cell;
import com.aspose.pdf.Document;
import com.aspose.pdf.Page;
import com.aspose.pdf.Row;
import com.aspose.pdf.Table;
import com.aspose.pdf.TextFragment;

public class TestAsposeV1 {

    /**
     * @param args
     * @throws IOException
     */
    public static void main(String[] args) throws IOException {
        Locale locale1 = new Locale("en");
        Locale.setDefault(locale1);

        // Create a new document
        Document doc = new Document();

        // Create a new table
        Table table = new Table();

        // Add a new page
        Page curPage = doc.getPages().add();

        // Add rows and cells to the table
        for (int rowIndex = 0; rowIndex < 10; rowIndex++) {
            Row row = table.getRows().add();
            for (int colIndex = 0; colIndex < 2; colIndex++) {
                Cell cell = row.getCells().add();
                TextFragment text = new TextFragment("Hai");
                cell.getParagraphs().add(text);
            }
        }

        // Add the table to the page
        curPage.getParagraphs().add(table);

        // Merge string HTML
        String stringHtml = "<font face=\"Times New Roman\" size=50>" +
                            "This is a test for HTML </ support " +
                            " in Text paragraph.";

        // Add HTML to the document
        aspose.pdf.Pdf htmlPdf = addHtml(stringHtml);

        // Save the document
        doc.save("c:/pdftest/HTMLString_to_PDF.pdf");
    }

    /**
     * Adds HTML to a PDF document
     *
     * @param stringHtml the HTML string
     * @return the PDF document with HTML
     */
    private static aspose.pdf.Pdf addHtml(String stringHtml) {
        aspose.pdf.Pdf pdf = new aspose.pdf.Pdf();
        aspose.pdf.Section sec = pdf.getSections().add();
        aspose.pdf.Text text1 = new aspose.pdf.Text(stringHtml);
        text1.isHtmlTagSupported(true);
        sec.getParagraphs().add(text1);
        return pdf;
    }
}

Hi,

Sincerely speaking I am very much confused with the usage of com.aspose.pdf and aspose.pdf packages . In one of the other issues that I had raised ,I was told that Aspose recommends the usage of new DOM model achieved by com.aspose.pdf package and Aspose highly recommends the usage of it and then many of the queries that is been answered is with the aspose.pdf package ,that too when we are using Aspose 4.5. ?

This problem is there when we try to create a com.aspose.pdf.document object from pdf object of aspose.pdf ,the existing com.aspose.pdf.document just get cleared. We even need not try to add it.

Do you have one consistent way to create pdf document from html data ,using just the com.aspose.pdf packages ?

When is this new Aspose 4.5 DOM model (com.aspose.pdf package) planned to become stable ? I hope when APIs are given to customers who buy it ,they expect it to work.There is too much too and fro communication in terms of issues that is simply slowing down our development.

Regards
Praveen

Hi Praveen,


Thanks for sharing the details.

I have observed that when concatenating the newly generated PDF document generated from HTML string, the table object already added to Document object do not appear in resultant PDF file. For the sake of correction, I have logged this problem
as PDFNEWJAVA-34000 in our issue tracking system. We will further
look into the details of this problem and will keep you updated on the status
of correction. Please be patient and spare us little time. We are sorry for
this inconvenience.
praveen.achuthan:
Sincerely speaking I am very much confused with the usage of com.aspose.pdf and aspose.pdf packages . In one of the other issues that I had raised ,I was told that Aspose recommends the usage of new DOM model achieved by com.aspose.pdf package and Aspose highly recommends the usage of it and then many of the queries that is been answered is with the aspose.pdf package ,that too when we are using Aspose 4.5. ?
Hi Praveen,

We do recommend the usage of new DOM of com.aspose.pdf package as all the new enhancements and corrections are being made in this package. However there are some features (i.e. HTML to PDF conversion) which are currently not supported by com.aspose.pdf package, therefore customers are using the legacy aspose.pdf package approach.