Set Temp PDF File Path

When working with IronPDF, you may find the program creating temporary files as you generate, modify, and render PDF documents. This process occurs as it would for any other software to temporarily store data while the program is running, and it may be necessary to ensure proper program functionality. With IronPDF, you will have full control over where this folder is created and other important settings related to the temporary files.

Steps to Setting Temp File Paths with IronPDF

using System;
using System.IO;
using IronPdf;

class Program
{
    static void Main()
    {
        // Define your custom temporary path
        var MyTempPath = @"C:\Safe\Location\";

        // Set environment variable for TEMP to your custom path
        Environment.SetEnvironmentVariable("TEMP", MyTempPath);

        // Set the IronPDF TempFolderPath
        Installation.TempFolderPath = Path.Combine(MyTempPath, "IronPdfTemp");

        // Create a new ChromePdfRenderer instance
        var renderer = new ChromePdfRenderer();

        // Render HTML string to a PDF document
        var doc = renderer.RenderHtmlAsPdf("<h1>Html with CSS and Images</h1>");

        // Save the PDF document to a file
        doc.SaveAs("example.pdf");

        Console.WriteLine("PDF Generated and saved successfully.");
    }
}
using System;
using System.IO;
using IronPdf;

class Program
{
    static void Main()
    {
        // Define your custom temporary path
        var MyTempPath = @"C:\Safe\Location\";

        // Set environment variable for TEMP to your custom path
        Environment.SetEnvironmentVariable("TEMP", MyTempPath);

        // Set the IronPDF TempFolderPath
        Installation.TempFolderPath = Path.Combine(MyTempPath, "IronPdfTemp");

        // Create a new ChromePdfRenderer instance
        var renderer = new ChromePdfRenderer();

        // Render HTML string to a PDF document
        var doc = renderer.RenderHtmlAsPdf("<h1>Html with CSS and Images</h1>");

        // Save the PDF document to a file
        doc.SaveAs("example.pdf");

        Console.WriteLine("PDF Generated and saved successfully.");
    }
}
Imports System
Imports System.IO
Imports IronPdf

Friend Class Program
	Shared Sub Main()
		' Define your custom temporary path
		Dim MyTempPath = "C:\Safe\Location\"

		' Set environment variable for TEMP to your custom path
		Environment.SetEnvironmentVariable("TEMP", MyTempPath)

		' Set the IronPDF TempFolderPath
		Installation.TempFolderPath = Path.Combine(MyTempPath, "IronPdfTemp")

		' Create a new ChromePdfRenderer instance
		Dim renderer = New ChromePdfRenderer()

		' Render HTML string to a PDF document
		Dim doc = renderer.RenderHtmlAsPdf("<h1>Html with CSS and Images</h1>")

		' Save the PDF document to a file
		doc.SaveAs("example.pdf")

		Console.WriteLine("PDF Generated and saved successfully.")
	End Sub
End Class
$vbLabelText   $csharpLabel

Explanation:

  1. Define Custom Path: We start by defining MyTempPath, a string variable containing the directory path where temporary files will be stored. Ensure this directory exists or has the necessary permissions, as IronPDF will use it during its operations.

  2. Set Environment Variables:

    • The Environment.SetEnvironmentVariable function is used to set the TEMP environment variable to point to our custom path MyTempPath. This is a common system-wide variable used by applications, including IronPDF, to determine where temporary files should be stored.
  3. Set IronPDF Temp Folder Path:

    • Installation.TempFolderPath property is set using Path.Combine to append "IronPdfTemp" to our custom path, creating a subdirectory specifically for IronPDF's temporary files.
  4. Generate PDF:
    • We instantiate a ChromePdfRenderer, which is used to convert HTML content to a PDF.
    • The method RenderHtmlAsPdf is called on the renderer object with a sample HTML string, which generates the PDF document stored in the doc variable.
    • We save the generated PDF to a file named "example.pdf" using doc.SaveAs.

By controlling the temporary file path, you can monitor and manage the files generated during PDF creation, ensuring that your application operates smoothly without cluttering default temporary directories.