LibreOffice-val dokumentum konvertálása PDF-re most jó
This commit is contained in:
@@ -1,35 +1,59 @@
|
||||
using DevExpress.XtraRichEdit;
|
||||
using System.Diagnostics;
|
||||
|
||||
namespace WorkFlowCheck.BL.DocumentGenerator
|
||||
{
|
||||
public class DokumentumGenerator
|
||||
{
|
||||
public static byte[] DocxToPdf(byte[] wordBytes)
|
||||
public static byte[] DocxToPdfL(byte[] wordBytes)
|
||||
{
|
||||
using (MemoryStream inputStream = new MemoryStream(wordBytes))
|
||||
using (MemoryStream outputStream = new MemoryStream())
|
||||
string pdfDirectory = Path.Combine(Directory.GetCurrentDirectory(), "Pdf");
|
||||
string fileName = Guid.NewGuid().ToString();
|
||||
|
||||
string tempWordPath = Path.Combine(pdfDirectory, fileName + ".docx");
|
||||
string tempPdfPath = Path.Combine(pdfDirectory, fileName + ".pdf");
|
||||
|
||||
try
|
||||
{
|
||||
try
|
||||
// Word fájl mentése ideiglenes fájlba
|
||||
File.WriteAllBytes(tempWordPath, wordBytes);
|
||||
|
||||
// LibreOffice parancssori konverzió
|
||||
ProcessStartInfo startInfo = new ProcessStartInfo
|
||||
{
|
||||
// RichEditDocumentServer használata a konverzióhoz
|
||||
using (RichEditDocumentServer wordProcessor = new RichEditDocumentServer())
|
||||
{
|
||||
// Dokumentum betöltése a byte tömbből
|
||||
wordProcessor.LoadDocument(inputStream, DevExpress.XtraRichEdit.DocumentFormat.OpenXml);
|
||||
FileName = "e:\\LibreOffice\\program\\soffice", // LibreOffice parancs
|
||||
Arguments = $"--headless --convert-to pdf \"{tempWordPath}\" --outdir \"{pdfDirectory}",
|
||||
RedirectStandardOutput = true,
|
||||
UseShellExecute = false,
|
||||
CreateNoWindow = true
|
||||
};
|
||||
|
||||
// PDF formátumba mentés a memóriafolyamba
|
||||
wordProcessor.ExportToPdf(outputStream);
|
||||
|
||||
// PDF byte tömb visszaadása
|
||||
return outputStream.ToArray();
|
||||
}
|
||||
using (Process process = Process.Start(startInfo))
|
||||
{
|
||||
process.WaitForExit();
|
||||
}
|
||||
catch (Exception ex)
|
||||
|
||||
// Ellenőrzés, hogy létrejött-e a PDF
|
||||
if (File.Exists(tempPdfPath))
|
||||
{
|
||||
Console.WriteLine("Hiba a konverzió során: " + ex.Message);
|
||||
return new byte[0];
|
||||
byte[] pdfBytes = File.ReadAllBytes(tempPdfPath);
|
||||
|
||||
// Ideiglenes fájlok törlése
|
||||
File.Delete(tempWordPath);
|
||||
File.Delete(tempPdfPath);
|
||||
|
||||
return pdfBytes;
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new Exception("PDF konverzió sikertelen.");
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine("Hiba a PDF konverzió során: " + ex.Message);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user