Errores al Guardar Archivos con Rutas Virtuales
En aplicaciones ASP.NET, PdfDocument.SaveAs() puede fallar con un error como este:
IronPDF could not write to the file '~/PDFFiles/mydocument.pdf'. It may be open in a PDF viewer.
El mensaje apunta a un archivo bloqueado, pero la causa real suele ser la ruta. IronPDF escribe en el disco y necesita una ruta física totalmente calificada, mientras que un valor como ~/PDFFiles/file.pdf es una ruta virtual ASP.NET que el sistema de archivos no puede resolver.
Mapea la ruta virtual a una ruta física
Convierta la ruta virtual antes de guardar, usando Server.MapPath() en ASP.NET Web Forms o HostingEnvironment.MapPath() en otro lugar:
string virtualPath = "~/PDFFiles/mydocument.pdf";
string physicalPath = Server.MapPath(virtualPath); // or HostingEnvironment.MapPath outside Web Forms
pdf.SaveAs(physicalPath);
string virtualPath = "~/PDFFiles/mydocument.pdf";
string physicalPath = Server.MapPath(virtualPath); // or HostingEnvironment.MapPath outside Web Forms
pdf.SaveAs(physicalPath);
Imports System.Web
Dim virtualPath As String = "~/PDFFiles/mydocument.pdf"
Dim physicalPath As String = Server.MapPath(virtualPath) ' or HostingEnvironment.MapPath outside Web Forms
pdf.SaveAs(physicalPath)
Un ejemplo mínimo que funciona:
var renderer = new ChromePdfRenderer();
PdfDocument pdf = renderer.RenderUrlAsPdf("https://example.com");
string physicalPath = Server.MapPath("~/App_Data/PDFFiles/output.pdf");
pdf.SaveAs(physicalPath);
var renderer = new ChromePdfRenderer();
PdfDocument pdf = renderer.RenderUrlAsPdf("https://example.com");
string physicalPath = Server.MapPath("~/App_Data/PDFFiles/output.pdf");
pdf.SaveAs(physicalPath);
Dim renderer As New ChromePdfRenderer()
Dim pdf As PdfDocument = renderer.RenderUrlAsPdf("https://example.com")
Dim physicalPath As String = Server.MapPath("~/App_Data/PDFFiles/output.pdf")
pdf.SaveAs(physicalPath)
Antes de guardar
- Directorio existe: crea la carpeta de destino y confirma que la aplicación tiene permiso de escritura en ella.
- Archivo no abierto: asegúrate de que la salida no esté ya abierta en un visor como Adobe Reader.
- Ubicaciones restringidas: ejecute con permisos suficientes al escribir en carpetas protegidas como
Program Files. - Verifica la ruta: registra la ruta física resuelta durante el desarrollo para confirmar que apunta donde esperas.

