LibreOfficeLibrary icon indicating copy to clipboard operation
LibreOfficeLibrary copied to clipboard

Using in ASP.NET

Open faaps opened this issue 5 years ago • 0 comments

LibreOffice needs a real environtment, when your library is used in ASP.net don't exists environtment for IIS users. The solution is create a fisical environtment ( in my case) in the temp directory. Your solution es so good, thanks for it.

public void ConvertToPdf(string filePath, string targetPath, string profileLocation = null) { //profileLocation = null; if (!File.Exists(filePath)) throw new ArgumentException("The file doesn't exist"); if (File.Exists(targetPath)) throw new ArgumentException("The target file exists");

        //if (profileLocation == null)
        //{
        //    var path = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
        //    var versionDir = Directory.GetDirectories(Path.Combine(path, "libreoffice")).FirstOrDefault();
        //    if (versionDir == null)
        //        return;

        //    profileLocation = versionDir;
        //}

        var profilePath = Path.GetDirectoryName(targetPath).Replace('\\', '/').Replace(" ", "%20");//Path.GetFullPath(profileLocation).Replace('\\', '/').Replace(" ", "%20");
        var tempDirPath = Path.GetDirectoryName(targetPath); //Path.GetTempPath().TrimEnd(Path.DirectorySeparatorChar);
        var tempFilePath = filePath;//Path.Combine(tempDirPath, Path.GetRandomFileName());
        var tempOutputFilePath = targetPath;

        //File.Copy(filePath, tempFilePath);

        var worker = new LibreOfficeWorker();
        worker.DoWork($"/C -headless -writer -convert-to pdf -outdir \"{tempDirPath}\" \"{tempFilePath}\" \"-env:UserInstallation=file:///{profilePath}/\"");


        //if (File.Exists(tempOutputFilePath))
        //    File.Move(tempOutputFilePath, targetPath);
    }

faaps avatar Jul 16 '19 22:07 faaps