> 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/convert-other.md).

# Convert to PDF with Virtual Printer on Server/Desktop for Windows: Sample Code in C#, Python, C++, Java, PHP, Ruby, and Go

Convert various file types to PDF with PDF Converter on Windows. Learn how to convert Word, HTML, RTF, and more to PDF using pdftron.PDF.Convert.ToPdf method. Find full sample code and supported forma

To convert different file types to PDF Documents, by acting as a virtual printer. Samples provided for C#, Python, C++, Java, PHP, Ruby, Go, VB; on Windows.

{% hint style="warning" %}
Only available on Microsoft Windows systems
{% endhint %}

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

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

// perform the conversion with no optional parameters
Convert.ToPdf(doc, filename);
```

{% endcode %}
{% endtab %}

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

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

// perform the conversion with no optional parameters
Convert::ToPdf(doc, filename);
```

{% endcode %}
{% endtab %}

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

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

// perform the conversion with no optional parameters
ConvertToPdf(doc, filename)
```

{% endcode %}
{% endtab %}

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

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

// perform the conversion with no optional parameters
Convert.toPdf(doc, filename);
```

{% endcode %}
{% endtab %}

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

```js
async function main() {
  const doc = await PDFNet.PDFDoc.create();

  // perform the conversion with no optional parameters
  await PDFNet.Convert.toPdf(doc, filename);
}
PDFNet.runWithCleanup(main);
```

{% endcode %}
{% endtab %}

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

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

// perform the conversion with no optional parameters
Convert.toPdf(doc, filename)
```

{% endcode %}
{% endtab %}

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

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

// perform the conversion with no optional parameters
[PTConvert ToPdf:doc in_filename:filename];
```

{% endcode %}
{% endtab %}

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

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

// perform the conversion with no optional parameters
PTConvert.toPdf(doc, in_filename: filename)
```

{% endcode %}
{% endtab %}

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

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

// perform the conversion with no optional parameters
Convert::ToPdf($doc, $filename);
```

{% endcode %}
{% endtab %}

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

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

# perform the conversion with no optional parameters
Convert.ToPdf(doc, filename)
```

{% endcode %}
{% endtab %}

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

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

# perform the conversion with no optional parameters
Convert.ToPdf(doc, filename)
```

{% endcode %}
{% endtab %}

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

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

' perform the conversion with no optional parameters
Convert.ToPdf(doc, filename)
```

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

[PDF Converter (SVG, XPS, TIFF, JPG, RTF, TXT, More)](/core/get-started/samples/converttest.md) Full sample code which shows how to convert any printable document (e.g. Word, HTML, RTF, MS Office, DXF, DWG, etc) to PDF. Samples available in Python, C# (.Net), C++, Go, Java, Node.js (JavaScript), PHP, Ruby, VB.

## About converting to PDF

The `pdftron.PDF.Convert.ToPdf` method (`ToPdf` for short, and relatedly, `ToXod` or `ToXps`), converts multiple file formats to the requested file output format. It supports several input formats like docx, xlsx, rtf, txt, html, pub, etc.

Formats that require external applications for conversion use the Printer class and the PDFNet printer to be installed. This is only supported on Windows platforms. Document formats in this category include RTF (MS Word or Wordpad), TXT (Notepad or Wordpad), DOC and DOCX (MS Word), PPT and PPTX (MS PowerPoint), XLS and XLSX (MS Excel), OpenOffice documents, HTML and MHT (Internet Explorer), PUB (MS Publisher), MSG (MS Outlook).

In order to convert these file formats the `ToPdf` method uses the PDFNet printer to request a print job, and the print output (in XPS) will be used for the actual conversion process.

It is important to keep in mind that in order to use the PDFNet printer, there must be an application installed on the machine, which will send print jobs to the PDFNet printer. The PDFNet printer works like any other printer; in order to print a document, an application must send printing jobs to the printer first, the PDFNet printer cannot open documents by itself.

***For example:*** Printing a text file requires a text file viewer to perform a printing task. In Windows, this is normally *notepad.exe*.

`ToPdf` automates this process. It issues the print command on a document, and expects that an installed application will process the command. If the application does not support printing of the opened document, then the `Convert.ToPdf` method will fail.

**Note:** It is important to understand that in a 64-bit operating system, the 64-bit PDFNet printer driver must be installed. Installing the 32-bit PDFNet printer in a 64-bit operating system will not work. To install the 64-bit PDFNet printer driver, use the 64-bit version of PDFNet.dll and invoke the following method in a .NET application:

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

```csharp
if (!Convert.Printer.IsInstalled())
{
  Convert.Printer.Install();
}
```

{% endcode %}
{% endtab %}

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

```cpp
if (!Convert::Printer::IsInstalled())
{
  Convert::Printer::Install();
}
```

{% endcode %}
{% endtab %}

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

```go
if PrinterIsInstalled("Apryse PDFNet")
{  
  PrinterInstall()
}
```

{% endcode %}
{% endtab %}

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

```java
if (!ConvertPrinter.isInstalled())
{
  ConvertPrinter.install();
}
```

{% endcode %}
{% endtab %}

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

```js
if (!PDFNet.Convert.printerIsInstalled())
{
  await PDFNet.Convert.printerInstall();
}
```

{% endcode %}
{% endtab %}

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

```kotlin
if (!ConvertPrinter.isInstalled())
{
  ConvertPrinter.install()
}
```

{% endcode %}
{% endtab %}

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

```objc
// Code snippet coming soon
```

{% endcode %}
{% endtab %}

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

```swift
// Code snippet coming soon
```

{% endcode %}
{% endtab %}

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

```php
if (!Convert::Printer::IsInstalled())
{
  Convert::Printer::Install();
}
```

{% endcode %}
{% endtab %}

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

```python
if not Convert.Printer.IsInstalled():
  Convert.Printer.Install()
```

{% endcode %}
{% endtab %}

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

```ruby
if !Convert::Printer::IsInstalled()
  Convert::Printer::Install()
end
```

{% endcode %}
{% endtab %}

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

```vb
If Not Convert.Printer.IsInstalled() Then
  Convert.Printer.Install()
End If
```

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

## MS Office short-cut

If Microsoft Office 2007 SP2 or later is installed, `ToPdf` method will take advantage of Microsoft Office’s OLE interop automation library to convert Microsoft Office documents to PDF or XPS formats. Using Microsoft Office guarantees high quality PDF or XPS output files.

The PDFNet printer will not be used when a document can be converted using the available Microsoft Office’s interop libraries. However; the converter will use the PDFNet printer if an earlier version of Microsoft Office is installed - any versions prior to 2007 SP2. This is because the ***SaveAsPDFandXPS*** extension is not available in the older versions of Microsoft Office.

## Special cases

Converting documents using Apryse, inside a Windows service, or an ASP.NET application requires some changes to the default settings that are used. Please read further to understand this better.

* [Working with a Windows service](/core/conversion/conversion/windows-service.md)
* [Working with an ASP.NET application](/core/conversion/conversion/asp-dotnet-app.md)


---

# 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 dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.apryse.com/core/conversion/convert-other.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
