Current section

Files

Jump to
chromic_pdf lib chromic_pdf api.ex
Raw

lib/chromic_pdf/api.ex

# SPDX-License-Identifier: Apache-2.0
defmodule ChromicPDF.API do
@moduledoc false
import ChromicPDF.{Telemetry, Utils}
alias ChromicPDF.{
Browser,
CaptureScreenshot,
ExportOptions,
GhostscriptPool,
PDFOptions,
PrintToPDF
}
@spec print_to_pdf(
ChromicPDF.Supervisor.services(),
ChromicPDF.source() | [ChromicPDF.source()],
[ChromicPDF.pdf_option() | ChromicPDF.export_option()]
) :: ChromicPDF.export_return()
def print_to_pdf(services, sources, opts) when is_list(sources) and is_list(opts) do
with_tmp_dir(fn tmp_dir ->
paths =
Enum.map(sources, fn source ->
tmp_path = Path.join(tmp_dir, random_file_name(".pdf"))
:ok = print_to_pdf(services, source, Keyword.put(opts, :output, tmp_path))
tmp_path
end)
output_path = Path.join(tmp_dir, random_file_name(".pdf"))
with_telemetry(:join_pdfs, opts, fn ->
:ok = GhostscriptPool.join(services.ghostscript_pool, paths, opts, output_path)
ExportOptions.feed_file_into_output(output_path, opts)
end)
end)
end
def print_to_pdf(services, %{source: source, opts: opts}, overrides)
when is_tuple(source) and is_list(opts) and is_list(overrides) do
print_to_pdf(services, source, Keyword.merge(opts, overrides))
end
def print_to_pdf(services, source, opts) when is_tuple(source) and is_list(opts) do
chrome_export(services, :print_to_pdf, source, opts)
end
@spec capture_screenshot(ChromicPDF.Supervisor.services(), ChromicPDF.source(), [
ChromicPDF.capture_screenshot_option() | ChromicPDF.export_option()
]) ::
ChromicPDF.export_return()
def capture_screenshot(services, %{source: source, opts: opts}, overrides)
when is_tuple(source) and is_list(opts) and is_list(overrides) do
capture_screenshot(services, source, Keyword.merge(opts, overrides))
end
def capture_screenshot(services, source, opts) when is_tuple(source) and is_list(opts) do
chrome_export(services, :capture_screenshot, source, opts)
end
@export_protocols %{
capture_screenshot: CaptureScreenshot,
print_to_pdf: PrintToPDF
}
defp chrome_export(services, protocol, source, opts) do
opts = PDFOptions.prepare_input_options(source, opts)
with_telemetry(protocol, opts, fn ->
services.browser
|> Browser.new_protocol(Map.fetch!(@export_protocols, protocol), opts)
|> ExportOptions.feed_chrome_data_into_output(opts)
end)
end
@spec convert_to_pdfa(ChromicPDF.Supervisor.services(), ChromicPDF.path(), [
ChromicPDF.pdfa_option() | ChromicPDF.export_option()
]) ::
ChromicPDF.export_return()
def convert_to_pdfa(services, pdf_path, opts) when is_binary(pdf_path) and is_list(opts) do
with_tmp_dir(fn tmp_dir ->
do_convert_to_pdfa(services, pdf_path, opts, tmp_dir)
end)
end
@spec print_to_pdfa(
ChromicPDF.Supervisor.services(),
ChromicPDF.source() | [ChromicPDF.source()],
[
ChromicPDF.pdf_option() | ChromicPDF.pdfa_option() | ChromicPDF.export_option()
]
) ::
ChromicPDF.export_return()
def print_to_pdfa(services, source, opts) when is_list(opts) do
with_tmp_dir(fn tmp_dir ->
pdf_path = Path.join(tmp_dir, random_file_name(".pdf"))
:ok = print_to_pdf(services, source, Keyword.put(opts, :output, pdf_path))
do_convert_to_pdfa(services, pdf_path, opts, tmp_dir)
end)
end
defp do_convert_to_pdfa(services, pdf_path, opts, tmp_dir) do
pdfa_path = Path.join(tmp_dir, random_file_name(".pdf"))
with_telemetry(:convert_to_pdfa, opts, fn ->
:ok = GhostscriptPool.convert(services.ghostscript_pool, pdf_path, opts, pdfa_path)
ExportOptions.feed_file_into_output(pdfa_path, opts)
end)
end
end