use std::collections::BTreeMap;
use base64::Engine;
use base64::engine::general_purpose::STANDARD;
use serde::{Deserialize, Serialize};
use serde_json::Value;
use crate::client::Bibby;
use crate::error::Result;
const PRODUCT: &str = "pdf";
#[derive(Debug, Clone, Copy)]
pub struct PdfClient<'a> {
client: &'a Bibby,
}
impl<'a> PdfClient<'a> {
pub(crate) fn new(client: &'a Bibby) -> Self {
Self { client }
}
pub async fn render(&self, request: &RenderPdfRequest) -> Result<RenderedPdf> {
self.client.post_json(PRODUCT, "pdfs/render", request).await
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum PageSize {
A4,
Letter,
A3,
None,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(untagged)]
pub enum UploaderSaveOption {
Default(bool),
Visibility {
public: bool,
},
}
impl UploaderSaveOption {
pub fn save() -> Self {
Self::Default(true)
}
pub fn public() -> Self {
Self::Visibility { public: true }
}
pub fn private() -> Self {
Self::Visibility { public: false }
}
}
#[derive(Debug, Clone, Default, Serialize)]
pub struct RenderPdfRequest {
#[serde(skip_serializing_if = "Option::is_none")]
pub html: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub url: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub data: Option<Value>,
#[serde(skip_serializing_if = "Option::is_none")]
pub css: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub images: Option<BTreeMap<String, String>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub fonts: Option<Vec<String>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub page_size: Option<PageSize>,
#[serde(skip_serializing_if = "Option::is_none")]
pub landscape: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
pub margin_mm: Option<f64>,
#[serde(skip_serializing_if = "Option::is_none")]
pub title: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub author: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub language: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub bookmarks: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
pub accessible: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
pub file_name: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub uploader: Option<UploaderSaveOption>,
}
impl RenderPdfRequest {
pub fn from_html(html: impl Into<String>) -> Self {
Self {
html: Some(html.into()),
..Self::default()
}
}
pub fn from_url(url: impl Into<String>) -> Self {
Self {
url: Some(url.into()),
..Self::default()
}
}
pub fn data(mut self, data: Value) -> Self {
self.data = Some(data);
self
}
pub fn css(mut self, css: impl Into<String>) -> Self {
self.css = Some(css.into());
self
}
pub fn image_bytes(mut self, name: impl Into<String>, bytes: impl AsRef<[u8]>) -> Self {
self.images
.get_or_insert_with(BTreeMap::new)
.insert(name.into(), STANDARD.encode(bytes.as_ref()));
self
}
pub fn font_bytes(mut self, bytes: impl AsRef<[u8]>) -> Self {
self.fonts
.get_or_insert_with(Vec::new)
.push(STANDARD.encode(bytes.as_ref()));
self
}
pub fn page_size(mut self, size: PageSize) -> Self {
self.page_size = Some(size);
self
}
pub fn landscape(mut self, landscape: bool) -> Self {
self.landscape = Some(landscape);
self
}
pub fn margin_mm(mut self, margin_mm: f64) -> Self {
self.margin_mm = Some(margin_mm);
self
}
pub fn file_name(mut self, file_name: impl Into<String>) -> Self {
self.file_name = Some(file_name.into());
self
}
pub fn save_to_uploader(mut self, option: UploaderSaveOption) -> Self {
self.uploader = Some(option);
self
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct SavedUploaderFile {
pub id: i64,
pub name: String,
pub public_url: Option<String>,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct RenderedPdf {
pub mime_type: String,
pub file_name: String,
pub size_bytes: i64,
pub data: String,
#[serde(default)]
pub warnings: Vec<String>,
pub uploader_file: Option<SavedUploaderFile>,
pub uploader_error: Option<String>,
}
impl RenderedPdf {
pub fn decode_bytes(&self) -> Result<Vec<u8>> {
Ok(STANDARD.decode(&self.data)?)
}
}