Browse Source

Convert to HTML and then to ODT (keeps images style)

master
Alex Puiu 2 years ago
parent
commit
d7891ae9c9
  1. 32
      app/Http/Controllers/FileController.php

32
app/Http/Controllers/FileController.php

@ -7,8 +7,8 @@ use Illuminate\Http\JsonResponse;
use Illuminate\Support\Facades\Storage;
use Illuminate\Http\UploadedFile;
use GuzzleHttp\Exception\BadResponseException;
use Illuminate\Support\Str;
use Symfony\Component\Process\Process;
use Symfony\Component\Process\Exception\ProcessFailedException;
class FileController extends Controller
{
@ -66,25 +66,22 @@ class FileController extends Controller
$fileId = request()->input('file_id');
Storage::disk('local')->put('tmp/' . $fileId . '.md', $mdFileContent);
$tmpMdFilePath = Storage::path('tmp/' . $fileId . '.md');
$tmpFilePath = Storage::path('tmp/' . $fileId);
$pandoc = new Process([
'pandoc',
'-f',
'markdown',
'-t',
'odt',
$tmpMdFilePath
]);
$pandoc->start();
while ($pandoc->isRunning()) {
//
$process = new Process(['pandoc', '-f', 'markdown_strict', $tmpFilePath . '.md', '-t', 'html']);
$process->run();
if (!$process->isSuccessful()) {
throw new ProcessFailedException($process);
}
$odtFileContent = $pandoc->getOutput();
Storage::disk('local')->put('tmp/' . $fileId . '.odt', $odtFileContent);
$output = str_replace('./contracts-images', config('app.url') . '/contracts-images', $process->getOutput());
Storage::disk('local')->put('tmp/' . $fileId . '.html', $output);
$process = new Process([ 'pandoc', '-f', 'html', $tmpFilePath . '.html', '-t', 'odt', '-o', $tmpFilePath . '.odt']);
$process->run();
if (!$process->isSuccessful()) {
throw new ProcessFailedException($process);
}
return response()->json([
'path' => 'tmp/' . $fileId . '.odt'
@ -112,6 +109,7 @@ class FileController extends Controller
$success = Storage::delete([
"tmp/{$id}.md",
"tmp/{$id}.odt",
"tmp/{$id}.html",
"contracts/{$id}.md",
]);
return response()->json(['success' => $success]);

Loading…
Cancel
Save