Converting HTML Documents to PDF Documents

This section describes how you can use the Generate PDF API to programmatically convert HTML documents to PDF documents.

Note: For more information about the Generate PDF service, see Services Reference for LiveCycle.

Summary of steps

To convert an HTML document to a PDF document, perform the following tasks:

  1. Include project files.

  2. Create a Generate PDF client.

  3. Retrieve the HTML content to convert to a PDF document.

  4. Convert the HTML content to a PDF document.

  5. Retrieve the results.

Include project files

Include necessary files into your development project. If you are creating a client application using Java, include the necessary JAR files. If you are using web services, ensure that you include the proxy files.

Create a Generate PDF client

Before you can programmatically perform a Generate PDF operation, you must create a Generate PDF service client. If you are using the Java API, create a GeneratePdfServiceClient object. If you are using the web service API, create a GeneratePDFServiceService.

Retrieve the HTML content to convert to a PDF document

Reference HTML content that you want to convert to a PDF document. You can reference HTML content such as an HTML file or HTML content that is accessible using a URL.

Convert the HTML content to a PDF document

After you create the service client, you can invoke the appropriate PDF creation operation. This operation needs information about the document to be converted, including the path to the target document.

Retrieve the results

After the HTML content is converted to a PDF document, you can retrieve the results and save the PDF document.

Convert HTML content to a PDF document using the Java API

Convert an HTML document to a PDF document using the Generate PDF API (Java):

  1. Include project files.

    Include client JAR files, such as adobe-generatepdf-client.jar, in your Java project’s class path.

  2. Create a Generate PDF client.

    Create a GeneratePdfServiceClient object by using its constructor and passing a ServiceClientFactory object that contains connection properties.

  3. Retrieve the HTML content to convert to a PDF document.

    Retrieve HTML content by creating a string variable and assigning a URL that points to HTML content.

  4. Convert the HTML content to a PDF document.

    Invoke the GeneratePdfServiceClient object’s htmlToPDF2 method and pass the following values:

    • A java.lang.String object that contains the URL of the HTML file to be converted.

    • A java.lang.String object that contains the file type settings to be used in the conversion. File type settings can include spidering levels.

    • A java.lang.String object that contains the name of the security settings to be used.

    • An optional com.adobe.idp.Document object that contains settings to be applied while generating the PDF document. If this information is not supplied, the settings are automatically chosen based on the previous three parameters.

    • An optional com.adobe.idp.Document object that contains metadata information to be applied to the PDF document.

  5. Retrieve the results.

    The htmlToPDF2 method returns an HtmlToPdfResult object that contains the new PDF document that was generated. To obtain the newly created PDF document, perform the following actions:

    • Invoke the HtmlToPdfResult object’s getCreatedDocument method. This returns a com.adobe.idp.Document object.

    • Invoke the com.adobe.idp.Document object’s copyToFile method to extract the PDF document from the object created in the previous step.

Convert HTML content to a PDF document using the web service API

Convert HTML content to a PDF document by using the Generate PDF API (web service):

  1. Include project files.

    Create a Microsoft .NET project that uses MTOM. Ensure that you use the following WSDL definition: http://localhost:8080/soap/services/GeneratePDFService?WSDL&lc_version=9.0.1.

    Note: Replace localhost with the IP address of the server hosting LiveCycle.
  2. Create a Generate PDF client.

    • Create a GeneratePDFServiceClient object by using its default constructor.

    • Create a GeneratePDFServiceClient.Endpoint.Address object by using the System.ServiceModel.EndpointAddress constructor. Pass a string value that specifies the WSDL to the LiveCycle service (for example, http://localhost:8080/soap/services/GeneratePDFService?blob=mtom.) You do not need to use the lc_version attribute. However, specify ?blob=mtom.

    • Create a System.ServiceModel.BasicHttpBinding object by getting the value of the GeneratePDFServiceClient.Endpoint.Binding field. Cast the return value to BasicHttpBinding.

    • Set the System.ServiceModel.BasicHttpBinding object’s MessageEncoding field to WSMessageEncoding.Mtom. This value ensures that MTOM is used.

    • Enable basic HTTP authentication by performing the following tasks:

      • Assign the LiveCycle user name to the field GeneratePDFServiceClient.ClientCredentials.UserName.UserName.

      • Assign the corresponding password value to the field GeneratePDFServiceClient.ClientCredentials.UserName.Password.

      • Assign the constant value HttpClientCredentialType.Basic to the field BasicHttpBindingSecurity.Transport.ClientCredentialType.

      • Assign the constant value BasicHttpSecurityMode.TransportCredentialOnly to the field BasicHttpBindingSecurity.Security.Mode.

  3. Retrieve the HTML content to convert to a PDF document.

    Retrieve HTML content by creating a string variable and assigning a URL that points to HTML content.

  4. Convert the HTML content to a PDF document.

    Convert the HTML content to a PDF document by invoking the GeneratePDFServiceService object’s HtmlToPDF2 method and pass the following values:

    • A string that contains the HTML content to convert.

    • A java.lang.String object that contains the file type settings to be used in the conversion.

    • A string object that contains the security settings to be used.

    • An optional BLOB object that contains settings to be applied while generating the PDF document.

    • An optional BLOB object that contains metadata information to be applied to the PDF document.

    • An output parameter of type BLOB that is populated by the CreatePDF2 method. The CreatePDF2 method populates this object with the converted document. (This parameter value is required only for web service invocation).

  5. Retrieve the results.

    • Retrieve the converted PDF document by assigning the BLOB object’s MTOM field to a byte array. The byte array represents the converted PDF document. Ensure you use the BLOB object that is used as the output parameter for the HtmlToPDF2 method.

    • Create a System.IO.FileStream object by invoking its constructor and passing a string value that represents the file location of the converted PDF document.

    • Create a System.IO.BinaryWriter object by invoking its constructor and passing the System.IO.FileStream object.

    • Write the contents of the byte array to a PDF file by invoking the System.IO.BinaryWriter object’s Write method and passing the byte array.

// Ethnio survey code removed