export to vault, added pdf settings

This commit is contained in:
zsviczian
2025-01-18 13:20:00 +01:00
parent c35bd385fe
commit 3d3ce73fa1
5 changed files with 203 additions and 53 deletions

View File

@@ -1,21 +1,27 @@
import { PDFDocument, rgb } from '@cantoo/pdf-lib';
import { getEA } from 'src/core';
import { svgToBase64 } from './utils';
export type PDFPageAlignment = "center" | "top-left" | "top-center" | "top-right" | "bottom-left" | "bottom-center" | "bottom-right";
export type PDFPageMarginString = "none" | "tiny" | "normal";
interface PDFExportScale {
fitToPage: boolean;
zoom?: number;
}
export interface PDFMargin {
left: number;
right: number;
top: number;
bottom: number;
}
interface PDFPageProperties {
dimensions?: {width: number; height: number};
backgroundColor?: string;
margin?: {
left: number;
right: number;
top: number;
bottom: number;
};
margin: PDFMargin;
alignment: PDFPageAlignment;
}
interface PageDimensions {
@@ -40,6 +46,15 @@ export const STANDARD_PAGE_SIZES = {
export type PageSize = keyof typeof STANDARD_PAGE_SIZES;
export function getMarginValue(margin:PDFPageMarginString): PDFMargin {
switch(margin) {
case "none": return { left: 0, right: 0, top: 0, bottom: 0 };
case "tiny": return { left: 5, right: 5, top: 5, bottom: 5 };
case "normal": return { left: 20, right: 20, top: 20, bottom: 20 };
default: return { left: 20, right: 20, top: 20, bottom: 20 };
}
}
export function getPageDimensions(pageSize: PageSize, orientation: PageOrientation): PageDimensions {
const dimensions = STANDARD_PAGE_SIZES[pageSize];
return orientation === "portrait"
@@ -47,8 +62,6 @@ export function getPageDimensions(pageSize: PageSize, orientation: PageOrientati
: { width: dimensions.height, height: dimensions.width };
}
const DEFAULT_MARGIN = 20; // 20pt margins
interface SVGDimensions {
width: number;
height: number;
@@ -117,20 +130,15 @@ async function addSVGToPage(
export async function exportToPDF({
SVG,
scale = { fitToPage: true, zoom: 1 },
pageProps = {}
pageProps,
}: {
SVG: SVGSVGElement[];
scale?: PDFExportScale;
pageProps?: PDFPageProperties;
scale: PDFExportScale;
pageProps: PDFPageProperties;
}): Promise<ArrayBuffer> {
const margin = pageProps.margin ?? {
left: DEFAULT_MARGIN,
right: DEFAULT_MARGIN,
top: DEFAULT_MARGIN,
bottom: DEFAULT_MARGIN
};
const pageDim = pageProps.dimensions ?? getPageDimensions("A4", "portrait");
const margin = pageProps.margin;
const pageDim = pageProps.dimensions;
const pdfDoc = await PDFDocument.create();
for (const svg of SVG) {

View File

@@ -493,3 +493,22 @@ export const hasExcalidrawEmbeddedImagesTreeChanged = (sourceFile: TFile, mtime:
const fileList = getExcalidrawEmbeddedFilesFiletree(sourceFile, plugin);
return fileList.some(f=>f.stat.mtime > mtime);
}
export async function createOrOverwriteFile(app: App, path: string, content: string | ArrayBuffer): Promise<TFile> {
const file = app.vault.getAbstractFileByPath(normalizePath(path));
if(content instanceof ArrayBuffer) {
if(file && file instanceof TFile) {
await app.vault.modifyBinary(file, content);
return file;
} else {
return await app.vault.createBinary(path, content);
}
}
if (file && file instanceof TFile) {
await app.vault.modify(file, content);
return file;
} else {
return await app.vault.create(path, content);
}
}