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
Explanation:
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.Set Environment Variables:
- The
Environment.SetEnvironmentVariable
function is used to set theTEMP
environment variable to point to our custom pathMyTempPath
. This is a common system-wide variable used by applications, including IronPDF, to determine where temporary files should be stored.
- The
Set IronPDF Temp Folder Path:
Installation.TempFolderPath
property is set usingPath.Combine
to append "IronPdfTemp" to our custom path, creating a subdirectory specifically for IronPDF's temporary files.
- 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 thedoc
variable. - We save the generated PDF to a file named "example.pdf" using
doc.SaveAs
.
- We instantiate a
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.