Convertendo Documentos com o Microsoft Azure App Service

Este artigo fornece instruções detalhadas passo a passo para converter documentos PDF no Microsoft Azure usando Aspose.PDF for .NET e Azure App Service.

Pré-requisitos

  • Visual Studio 2022 Community Edition com desenvolvimento Azure instalado ou Visual Studio Code.
  • Conta Azure: Você precisa de uma assinatura Azure, crie uma conta gratuita antes de começar.
  • .NET 6 SDK.
  • Aspose.PDF for .NET.

Criar Recursos Azure

Criar App Service

  1. Vá para o Portal Azure (https://portal.azure.com).
  2. Crie um novo Grupo de Recursos.
  3. Crie um novo App Service:
    • Escolha o runtime .NET 6 (LTS).
    • Selecione o nível de preço apropriado.
  4. Crie um recurso Application Insights para registro.

Criar Projeto

Criar Projeto no Visual Studio

  1. Abra o Visual Studio 2022.
  2. Clique em “Criar um novo projeto”.
  3. Selecione “ASP.NET Core Web API”.
  4. Nomeie seu projeto como “PdfConversionService”.
  5. Selecione “.NET 6.0” ou posterior.
  6. Clique em “Criar”.

Criar Projeto no Visual Studio Code

Instalar Pré-requisitos

  1. Extensões do Visual Code:
code --install-extension ms-dotnettools.csharp
code --install-extension ms-azuretools.vscode-azureappservice
  1. Instale o Azure CLI:
  • Windows: Baixe do site da Microsoft.
  • macOS: brew install azure-cli.
  • Linux: curl -sL https://aka.ms/InstallAzureCLIDeb | sudo bash.

Configurar Projeto

  1. Abra o projeto no Visual Studio Code:
code .
  1. Adicione pacotes NuGet criando/atualizando PdfConverterApp.csproj:
<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>net6.0</TargetFramework>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Aspose.PDF" Version="24.10.0" />
    <PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.22.0" />
    <PackageReference Include="Microsoft.Extensions.Logging.AzureAppServices" Version="8.0.10" />
  </ItemGroup>
</Project>
  1. Adicione configuração:
// .vscode/launch.json
{
    "version": "0.2.0",
    "configurations": [
        {
            "name": ".NET Core Launch (web)",
            "type": "coreclr",
            "request": "launch",
            "preLaunchTask": "build",
            "program": "${workspaceFolder}/bin/Debug/net6.0/PdfConversionService.dll",
            "args": [],
            "cwd": "${workspaceFolder}",
            "stopAtEntry": false,
            "env": {
                "ASPNETCORE_ENVIRONMENT": "Development"
            }
        }
    ]
}
  1. Crie a estrutura do projeto:
mkdir Controllers
touch Controllers/PdfController.cs

Instalar Pacotes NuGet Necessários

No Visual Studio, abra o Console do Gerenciador de Pacotes e execute:

Install-Package Aspose.PDF
Install-Package Microsoft.ApplicationInsights.AspNetCore
Install-Package Microsoft.Extensions.Logging.AzureAppServices

No Visual Studio Code, execute:

dotnet restore

Configurar Licença Aspose

No Visual Studio:

  1. Copie seu arquivo de licença Aspose.PDF para o projeto.
  2. Clique com o botão direito no arquivo de licença e selecione “Propriedades”.
  3. Defina “Copiar para o Diretório de Saída” como “Copiar sempre”.
  4. Adicione o código de inicialização da licença no Program.cs:
var license = new Aspose.Pdf.License();
license.SetLicense("Aspose.PDF.lic");

Criar código

No Visual Studio:

  1. Clique com o botão direito na pasta Controllers.
  2. Adicione → Novo Item → Controlador API - Vazio.
  3. Nomeie seu arquivo como “PdfController.cs”.
// PdfController.cs
using Microsoft.AspNetCore.Mvc;

[ApiController]
[Route("api/[controller]")]
public class PdfController : ControllerBase
{
    private readonly ILogger<PdfController> _logger;

    public PdfController(ILogger<PdfController> logger)
    {
        _logger = logger;
    }

    [HttpPost("convert")]
    public async Task<IActionResult> ConvertPdf(
        IFormFile file,
        [FromQuery] string outputFormat = "docx")
    {
        try
        {
            if (file == null || file.Length == 0)
            {
                return BadRequest("No file uploaded");
            }

            // Validate input file is PDF
            if (!file.ContentType.Equals("application/pdf", StringComparison.OrdinalIgnoreCase))
            {
                return BadRequest("File must be a PDF");
            }

            using var inputStream = file.OpenReadStream();
            using var document = new Aspose.Pdf.Document(inputStream);
            using var outputStream = new MemoryStream();

            switch (outputFormat.ToLower())
            {
                case "docx":
                    document.Save(outputStream, Aspose.Pdf.SaveFormat.DocX);
                    return File(outputStream.ToArray(),
                        "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
                        "converted.docx");

                case "html":
                    document.Save(outputStream, Aspose.Pdf.SaveFormat.Html);
                    return File(outputStream.ToArray(),
                        "text/html",
                        "converted.html");

                case "jpg":
                case "jpeg":
                    var jpegDevice = new Aspose.Pdf.Devices.JpegDevice();
                    jpegDevice.Process(document.Pages[1], outputStream);
                    return File(outputStream.ToArray(),
                        "image/jpeg",
                        "converted.jpg");

                case "png":
                    var pngDevice = new Aspose.Pdf.Devices.PngDevice();
                    pngDevice.Process(document.Pages[1], outputStream);
                    return File(outputStream.ToArray(),
                        "image/png",
                        "converted.png");

                default:
                    return BadRequest("Unsupported output format");
            }
        }
        catch (Exception ex)
        {
            _logger.LogError(ex, "Error converting PDF");
            return StatusCode(500, "Internal server error");
        }
    }
}
// Program.cs
var builder = WebApplication.CreateBuilder(args);

builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

// Add logging
builder.Services.AddApplicationInsightsTelemetry();
builder.Services.AddLogging(logging =>
{
    logging.AddConsole();
    logging.AddDebug();
    logging.AddAzureWebAppDiagnostics();
});

var app = builder.Build();

// Initialize license
var license = new Aspose.Pdf.License();
license.SetLicense("Aspose.PDF.lic");

if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI();
}

app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
app.Run();

Configurar configurações do aplicativo

  1. Abra appsettings.json.
  2. Adicione configuração:
{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft.AspNetCore": "Warning"
    }
  },
  "AllowedHosts": "*",
  "ApplicationInsights": {
    "ConnectionString": "Your-Connection-String"
  }
}

Substitua Your-Connection-StringG pela sua string de conexão real do Portal Azure.

Testar Localmente

No Visual Studio:

  1. Pressione F5 para executar o aplicativo.
  2. A interface do Swagger será aberta.
  3. Teste o endpoint /api/pdf/convert:
    • Clique em “Experimente”.
    • Faça o upload de um arquivo PDF.
    • Selecione o formato de saída.
    • Execute e verifique a conversão.

No Visual Studio Code:

dotnet run

curl -X POST "https://localhost:5001/api/pdf/convert?outputFormat=docx" \
     -F "file=@sample.pdf" \
     -o converted.docx

Implantar no Azure

No Visual Studio:

  1. Clique com o botão direito no projeto.
  2. Selecione “Publicar”.
  3. Escolha “Azure” como destino.
  4. Selecione “Azure App Service (Windows)”.
  5. Selecione sua assinatura e App Service.
  6. Clique em “Publicar”.

No Visual Studio Code:

dotnet publish -c Release

az webapp deployment source config-zip \
    --resource-group $resourceGroup \
    --name $appName \
    --src bin/Release/net6.0/publish.zip

az webapp deploy \
    --resource-group $resourceGroup \
    --name $appName \
    --src-path "Aspose.PDF.lic" \
    --target-path "site/wwwroot/Aspose.PDF.lic"

Configurar Azure App Service

  1. Vá para o Portal Azure.
  2. Abra seu App Service.
  3. Configure as configurações:
    App Settings:
    - WEBSITE_RUN_FROM_PACKAGE=1
    - ASPNETCORE_ENVIRONMENT=Production
    

Testar o Serviço Implantado

Use Postman ou curl para testar:

curl -X POST "https://your-app.azurewebsites.net/api/pdf/convert?outputFormat=docx" \
     -F "file=@sample.pdf" \
     -o converted.docx

Formatos Suportados

A lista de formatos suportados pode ser encontrada aqui.

Solução de Problemas

Opções de Configuração Importantes

  1. Limites de Tamanho de Arquivo Adicione ao web.config:
<configuration>
  <system.webServer>
    <security>
      <requestFiltering>
        <requestLimits maxAllowedContentLength="104857600" />
      </requestFiltering>
    </security>
  </system.webServer>
</configuration>
  1. CORS (se necessário) No Program.cs:
builder.Services.AddCors(options =>
{
    options.AddPolicy("AllowSpecificOrigin",
        builder => builder
            .WithOrigins("https://your-frontend-domain.com")
            .AllowAnyMethod()
            .AllowAnyHeader());
});
  1. Autenticação (se necessário)
builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
    .AddJwtBearer(options => {
        // Configure JWT options
    });