C#에서 PDF를 SVG로 변환하는 방법 (튜토리얼)
Source: Dev.to
소개
이 튜토리얼에서는 호스팅된 BuildVu Cloud API를 사용하여 C#에서 PDF 파일을 SVG로 변환하는 방법을 설명합니다. 예시로는 다음과 같은 경우를 포함합니다.
- IDRsolutions 체험판 또는 클라우드 구독
- 자체 호스팅 BuildVu 마이크로서비스 인스턴스
서비스는 표준 HTTP 요청으로도 접근할 수 있지만, 이 튜토리얼에서는 오픈소스 C# IDRCloudClient를 사용합니다. 이 클라이언트는 REST API를 손쉽게 사용할 수 있는 C# 래퍼를 제공합니다.
사전 요구 사항
NuGet을 통해 idrsolutions-csharp-client 패키지를 설치합니다:
nuget install idrsolutions-csharp-client
코드 예제
아래는 PDF 파일을 SVG로 변환하는 간단한 코드 예제입니다. 추가 구성 옵션 및 고급 기능은 아래쪽에서 자세히 설명합니다.
using System;
using System.Collections.Generic;
using idrsolutions_csharp_client;
class ExampleUsage
{
static void Main(string[] args)
{
var client = new IDRCloudClient("https://cloud.idrsolutions.com/cloud/" + IDRCloudClient.BUILDVU);
try
{
Dictionary parameters = new Dictionary
{
//["token"] = "Token", // Required only when connecting to the IDRsolutions trial and cloud subscription service
["input"] = IDRCloudClient.UPLOAD,
["file"] = "path/to/input.pdf"
};
Dictionary results = client.Convert(parameters);
string outputUrl = results.GetValueOrDefault("downloadUrl", "No download URL provided");
client.DownloadResult(results, "path/to/output/dir");
Console.WriteLine("Converted: " + outputUrl);
}
catch (Exception e)
{
Console.WriteLine("Conversion failed: " + e.Message);
}
}
}
콜백 URL에 결과 반환
BuildVu 마이크로서비스는 변환이 완료되면 상태를 보고하기 위해 콜백 URL을 받을 수 있습니다. 콜백 URL을 사용하면 서비스를 폴링할 필요가 없습니다. 아래와 같이 convert 메서드에 콜백 URL을 제공하십시오.
Dictionary parameters = new Dictionary
{
//["token"] = "Token", // Required only when connecting to the IDRsolutions trial and cloud subscription service
["callbackUrl"] = "http://listener.url",
["input"] = IDRCloudClient.UPLOAD,
["file"] = "path/to/input.pdf"
};
구성 옵션
BuildVu API는 문자열 형태의 JSON 객체를 받아 키‑값 쌍으로 구성 옵션을 지정할 수 있습니다. 이러한 설정을 parameters 딕셔너리에 포함시키세요. PDF 파일을 SVG로 변환하기 위한 전체 구성 옵션 목록은 여기에서 확인할 수 있습니다.
["settings"] = "{\"key\":\"value\",\"key\":\"value\"}"
URL로 업로드
로컬 파일을 업로드하는 대신 BuildVu 마이크로서비스가 다운로드하고 변환하도록 URL을 제공할 수 있습니다. input 및 file 항목을 다음과 같이 교체하십시오.
["input"] = IDRCloudClient.DOWNLOAD,
["url"] = "http://exampleURL/exampleFile.pdf"
인증 사용
자체 호스팅 BuildVu 마이크로서비스가 사용자 이름과 비밀번호를 요구하는 경우, 클라이언트 인스턴스를 생성할 때 이를 포함합니다:
var client = new IDRCloudClient("http://exampleURL.com/" + IDRCloudClient.BUILDVU, "username", "password");