mirror of
https://github.com/TheAnachronism/docspell.git
synced 2025-03-28 17:55:06 +00:00
Early draft for running wkhtmltopdf
This commit is contained in:
parent
e0682464b5
commit
c665c212a0
50
modules/convert/src/main/scala/docspell/convert/extern/WkHtmlPdf.scala
vendored
Normal file
50
modules/convert/src/main/scala/docspell/convert/extern/WkHtmlPdf.scala
vendored
Normal file
@ -0,0 +1,50 @@
|
||||
package docspell.convert.extern
|
||||
|
||||
import java.nio.file.Path
|
||||
|
||||
import cats.effect._
|
||||
import fs2.{Pipe, Stream}
|
||||
import docspell.common._
|
||||
|
||||
object WkHtmlPdf {
|
||||
|
||||
def toPDF[F[_]: Sync: ContextShift](
|
||||
cfg: WkHtmlPdfConfig,
|
||||
chunkSize: Int,
|
||||
blocker: Blocker,
|
||||
logger: Logger[F]
|
||||
): Pipe[F, Byte, Byte] =
|
||||
in =>
|
||||
Stream.resource(File.withTempDir[F](cfg.workingDir, "docspell-wkhtmltopdf")).flatMap { dir =>
|
||||
val out = dir.resolve("out.pdf")
|
||||
val sysCfg =
|
||||
cfg.cmd.mapArgs(_.replace("{{outfile}}", out.toAbsolutePath.normalize.toString))
|
||||
|
||||
Stream.eval(logger.info(s"Running ${sysCfg.program}")).drain ++
|
||||
SystemCommand
|
||||
.execSuccess[F](sysCfg, blocker, Some(dir), in)
|
||||
.flatMap(result => readResult[F](out, result, blocker, chunkSize, logger))
|
||||
}
|
||||
|
||||
private def readResult[F[_]: Sync: ContextShift](
|
||||
out: Path,
|
||||
result: SystemCommand.Result,
|
||||
blocker: Blocker,
|
||||
chunkSize: Int,
|
||||
logger: Logger[F]
|
||||
): Stream[F, Byte] =
|
||||
Stream.eval(File.existsNonEmpty[F](out)).flatMap {
|
||||
case true =>
|
||||
if (result.rc == 0) File.readAll(out, blocker, chunkSize)
|
||||
else
|
||||
Stream
|
||||
.eval(logger.warn(s"Command not successful (rc=${result.rc}), but file exists."))
|
||||
.drain ++
|
||||
File.readAll(out, blocker, chunkSize)
|
||||
|
||||
case false =>
|
||||
Stream.raiseError(
|
||||
new Exception(s"Command result=${result.rc}. No output file found. ${result.stderr}")
|
||||
)
|
||||
}
|
||||
}
|
7
modules/convert/src/main/scala/docspell/convert/extern/WkHtmlPdfConfig.scala
vendored
Normal file
7
modules/convert/src/main/scala/docspell/convert/extern/WkHtmlPdfConfig.scala
vendored
Normal file
@ -0,0 +1,7 @@
|
||||
package docspell.convert.extern
|
||||
|
||||
import java.nio.file.Path
|
||||
|
||||
import docspell.common.SystemCommand
|
||||
|
||||
case class WkHtmlPdfConfig (cmd: SystemCommand.Config, workingDir: Path)
|
Loading…
x
Reference in New Issue
Block a user