> For the complete documentation index, see [llms.txt](https://docs.apryse.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.apryse.com/core/conversion/web-html-to-pdf/convert-html.md).

# Convert HTML to PDF on Server/Desktop: Sample Code in Python, Java, C#, JavaScript, PHP, and C++

Use Apryse Server SDK to convert HTML to pdf.  Samples offered in Python, Java, .NET(C#), node.js (JavaScript), php, Ruby, C++, Go, VB; for Mac, Linux and Windows.

HTML2PDF Module is an optional add-on utility which can be used with Apryse SDK to convert an HTML file to a PDF document without any external third party dependencies. Samples offered in Python, Java, C#, JavaScript, PHP, Ruby, C++, Objective C, Go, VB; for Mac, Linux and Windows.

To use this code, you'll need to

1. [Download and get started with Server SDK](/core/get-started/get-started.md)
2. [Install the HTML2PDF Module](/core/learn-more/modules.md#html2pdf-module)

{% hint style="warning" %}
**Warning**

Requires the HTML2PDF module add-on. All files in the `Lib` folder of the archive must be present for the module to work as expected.
{% endhint %}

{% tabs %}
{% tab title="C#" %}
{% code lineNumbers="true" %}

```csharp
// Start with a PDFDoc (the conversion destination)
PDFDoc doc = new PDFDoc();
HTML2PDF converter = new HTML2PDF();

// add from HTML String data
converter.InsertFromHtmlString(htmlString);

// add from a web page location
converter.InsertFromURL(webPageLocation);

// apply conversion
converter.Convert(doc);
```

{% endcode %}
{% endtab %}

{% tab title="C++" %}
{% code lineNumbers="true" %}

```cpp
// Start with a PDFDoc (the conversion destination)
PDFDoc doc;
HTML2PDF converter;

// add from HTML String data
converter.InsertFromHtmlString(htmlString);

// add from a web page location
converter.InsertFromURL(webPageLocation);

// apply conversion
converter.Convert(doc);
```

{% endcode %}
{% endtab %}

{% tab title="Go" %}
{% code lineNumbers="true" %}

```go
// Start with a PDFDoc (the conversion destination)
doc := NewPDFDoc()
converter := NewHTML2PDF()

// add from HTML String data
converter.InsertFromHtmlString(htmlString)

// add from a web page location
converter.InsertFromURL(webPageLocation)

// apply conversion
converter.Convert(doc)
```

{% endcode %}
{% endtab %}

{% tab title="Java" %}
{% code lineNumbers="true" %}

```java
// Start with a PDFDoc (the conversion destination)
PDFDoc doc = new PDFDoc();
HTML2PDF converter = new HTML2PDF();

// add from HTML String data
converter.insertFromHtmlString(htmlString);

// add from a web page location
converter.insertFromURL(webPageLocation);

// apply conversion
converter.convert(doc);
```

{% endcode %}
{% endtab %}

{% tab title="JavaScript" %}
{% code lineNumbers="true" %}

```js
async function main() {
  // Start with a PDFDoc (the conversion destination)
  const doc = await PDFNet.PDFDoc.create();
  const converter = await PDFNet.HTML2PDF.create();

  // add from HTML String data
  converter.insertFromHtmlString(htmlString);

  // add from a web page location
  converter.insertFromUrl(webPageLocation);

  // apply conversion
  await converter.convert(doc);
}
PDFNet.runWithCleanup(main);
```

{% endcode %}
{% endtab %}

{% tab title="Kotlin" %}
{% code lineNumbers="true" %}

```kotlin
// Start with a PDFDoc (the conversion destination)
val doc = PDFDoc()
val converter = HTML2PDF()

// add from HTML String data
converter.insertFromHtmlString(htmlString)

// add from a web page location
converter.insertFromURL(webPageLocation)

// apply conversion
converter.convert(doc)
```

{% endcode %}
{% endtab %}

{% tab title="Obj-C" %}
{% code lineNumbers="true" %}

```objc
// Start with a PDFDoc (the conversion destination)
PTPDFDoc* doc = [[PTPDFDoc alloc] init];
PTHTML2PDF *converter = [[PTHTML2PDF alloc] init];

// add from HTML String data
[converter InsertFromHtmlString: htmlString];

// add from a web page location
[converter InsertFromURL: webPageLocation];

// apply conversion
[converter Convert: doc];
```

{% endcode %}
{% endtab %}

{% tab title="Swift" %}
{% code lineNumbers="true" %}

```swift
// Start with a PDFDoc (the conversion destination)
let doc: PTPDFDoc = PTPDFDoc()
let converter: PTHTML2PDF = PTHTML2PDF()

// add from HTML String data
converter.insertFromHtmlString(htmlString)

// add from a web page location
converter.insertFromURL(webPageLocation)

// apply conversion
converter.convert(doc)
```

{% endcode %}
{% endtab %}

{% tab title="PHP" %}
{% code lineNumbers="true" %}

```php
// Start with a PDFDoc (the conversion destination)
$doc = new PDFDoc();
$converter = new HTML2PDF();

// add from HTML String data
converter->InsertFromHtmlString($htmlString);

// add from a web page location
converter->InsertFromURL($webPageLocation);

// apply conversion
converter->Convert($doc);
```

{% endcode %}
{% endtab %}

{% tab title="Python" %}
{% code lineNumbers="true" %}

```python
# Start with a PDFDoc (the conversion destination)
doc = PDFDoc()
converter = HTML2PDF()

# add from HTML String data
converter.InsertFromHtmlString(htmlString)

# add from a web page location
converter.InsertFromURL(webPageLocation)

# apply conversion
converter.Convert(doc)
```

{% endcode %}
{% endtab %}

{% tab title="Ruby" %}
{% code lineNumbers="true" %}

```ruby
# Start with a PDFDoc (the conversion destination)
doc = PDFDoc.new()
converter = HTML2PDF.new()

# add from HTML String data
converter.InsertFromHtmlString(htmlString)

# add from a web page location
converter.InsertFromURL(webPageLocation)

# apply conversion
converter.Convert(doc)
```

{% endcode %}
{% endtab %}

{% tab title="VB" %}
{% code lineNumbers="true" %}

```vb
' Start with a PDFDoc (the conversion destination)
Dim doc As PDFDoc = New PDFDoc()
Dim converter As HTML2PDF = New HTML2PDF()

' add from HTML String data
converter.InsertFromHtmlString(htmlString)

' add from a web page location
converter.InsertFromURL(webPageLocation)

' apply conversion
converter.Convert(doc)
```

{% endcode %}
{% endtab %}
{% endtabs %}

[Convert HTML to PDF - Full Sample Code](/core/get-started/samples/html2pdftest.md) Full sample code for directly converting HTML pages to PDF by using 'pdftron.PDF.HTML2PDF'. The HTML2PDF converter supports HTML conversion from a string or URL and offers many options to control page size and formatting. Samples available in Python, C# (.Net), C++, Go, Java, Node.js (JavaScript), PHP, Ruby, VB.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.apryse.com/core/conversion/web-html-to-pdf/convert-html.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
