PdfExportAPI.cs
2.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
using System;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using PdfExportOpri.Common;
using System.Diagnostics;
using System.IO;
using System.Threading.Tasks;
using Microsoft.Net.Http.Headers;
using Zircon.Base.Exceptions;
namespace PdfExportOpri.Modules.PdfExport
{
[ApiController]
public class PdfExportApi : ControllerBase
{
private readonly ILogger<PdfExportApi> _logger;
private readonly IConfiguration _config;
public PdfExportApi(ILogger<PdfExportApi> logger, IConfiguration config)
{
_logger = logger;
_config = config;
}
[HttpGet]
[Route("/pdf/web/api/v1/export")]
public async Task<FileResult> Get()
{
string id = CommonClass.GetRequest(HttpContext, "id", "", true);
ProcessStartInfo info = new ProcessStartInfo(OperatingSystem.IsWindows()?AppDomain.CurrentDomain.BaseDirectory+"./PDFExportOpri-CLI.exe": AppDomain.CurrentDomain.BaseDirectory + "./PDFExportOpri-CLI");
info.Arguments = $"{AppDomain.CurrentDomain.BaseDirectory}./output/ {id}";
info.RedirectStandardOutput = true;
info.RedirectStandardError = true;
Process p = new Process();
p.StartInfo = info;
p.Start();
await p.WaitForExitAsync();
if (p.ExitCode != 0)
throw new HttpException(){ErrorMessage =
p.StandardOutput.ReadToEnd(),
HttpCode = 500,
Number = "500.1"
};
Response.Headers.Add("Content-Disposition", "filename=export.pdf");
using (FileStream fsRead = new FileStream(AppDomain.CurrentDomain.BaseDirectory + $"./output/export_{id}.pdf", FileMode.Open))
{
int fsLen = (int)fsRead.Length;
byte[] heByte = new byte[fsLen];
int r = fsRead.Read(heByte, 0, heByte.Length);
Stream stream = new MemoryStream(heByte);
fsRead.Close();
System.IO.File.Delete(AppDomain.CurrentDomain.BaseDirectory + $"./output/export_{id}.pdf");
return new FileStreamResult(stream, "application/octet-stream");
}
// return new PhysicalFileResult(AppDomain.CurrentDomain.BaseDirectory + $"./output/export_{id}.pdf", "application/octet-stream");
}
}
}