The OnlyOffice Control provides robust PDF conversion capabilities through the OnlyOffice Document Server conversion service. This guide covers converting both original and edited documents to PDF format.
The conversion service URL is derived from the OnlyOffice API URL (OnlyOfficeEditor.ascx.cs:298-305):
private string ResolveConvertServiceUrl(){ if (string.IsNullOrWhiteSpace(OnlyOfficeApiUrl) || !Uri.TryCreate(OnlyOfficeApiUrl, UriKind.Absolute, out var apiUri)) throw new InvalidOperationException("OnlyOfficeApiUrl no está configurada correctamente."); var root = apiUri.GetLeftPart(UriPartial.Authority); return root.TrimEnd('/') + "/ConvertService.ashx";}
Example:
API URL: https://docserver.com/web-apps/apps/api/documents/api.js
The retry logic is implemented in ConvertDocumentSourceToPdfUrl (OnlyOfficeEditor.ascx.cs:163-207):
1
Validate Parameters
2
if (string.IsNullOrWhiteSpace(sourceUrl) || !Uri.TryCreate(sourceUrl, UriKind.Absolute, out _)) throw new InvalidOperationException("La URL del documento no es válida para la conversión.");var sourceExt = Path.GetExtension(sourceName)?.TrimStart('.').ToLowerInvariant();if (string.IsNullOrWhiteSpace(sourceExt)) throw new InvalidOperationException("No fue posible determinar el tipo del documento.");
3
Initialize Retry Loop
4
var attempts = Math.Max(1, maxAttempts);var wait = Math.Max(0, delayMs);for (var i = 0; i < attempts; i++){ // Conversion attempt}
5
Make Conversion Request
6
var result = CallConvertService(convertServiceUrl, body, token);if (result.EndConvert && !string.IsNullOrWhiteSpace(result.FileUrl)) return result.FileUrl;
7
Handle Errors
8
if (!string.IsNullOrWhiteSpace(result.ErrorMessage)) throw new InvalidOperationException(result.ErrorMessage);
9
Delay Between Attempts
10
if (i < attempts - 1 && wait > 0) Thread.Sleep(wait);
11
Timeout
12
throw new TimeoutException("La conversión a PDF no terminó dentro del tiempo esperado.");
The conversion service response is parsed to extract results (OnlyOfficeEditor.ascx.cs:351-388):
private static ConvertServiceResult ParseConvertServiceResult(string json){ var result = new ConvertServiceResult(); if (string.IsNullOrWhiteSpace(json)) { result.ErrorMessage = "La respuesta del servicio de conversión llegó vacía."; return result; } var serializer = new JavaScriptSerializer(); var payload = serializer.Deserialize<Dictionary<string, object>>(json) ?? new Dictionary<string, object>(); if (payload.TryGetValue("fileUrl", out var fileUrlObj)) result.FileUrl = fileUrlObj as string; if (payload.TryGetValue("endConvert", out var endConvertObj)) { try { result.EndConvert = Convert.ToBoolean(endConvertObj); } catch { result.EndConvert = false; } } if (payload.TryGetValue("error", out var errorObj) && errorObj != null) { var errorRaw = Convert.ToString(errorObj); if (!string.IsNullOrWhiteSpace(errorRaw) && errorRaw != "0") result.ErrorMessage = "ConvertService devolvió error=" + errorRaw + "."; } return result;}
if (!HasDocument) throw new InvalidOperationException("No hay un documento cargado para convertir.");
Ensure a document is loaded before attempting conversion.
No Edited Document
var editedBytes = GetEditedDocumentBytes();if (editedBytes == null || editedBytes.Length == 0) throw new InvalidOperationException( "No se encontró un documento editado capturado para convertir.");
Capture the edited document before converting.
Invalid Document URL
if (string.IsNullOrWhiteSpace(sourceUrl) || !Uri.TryCreate(sourceUrl, UriKind.Absolute, out _)) throw new InvalidOperationException( "La URL del documento no es válida para la conversión.");
Verify that document URLs are properly configured.
Conversion Timeout
throw new TimeoutException( "La conversión a PDF no terminó dentro del tiempo esperado.");
Increase maxAttempts or check OnlyOffice server status.