Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[office template] 페이지가 1개일때 푸터나오지 않는 버그 수정 #5

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 0 additions & 6 deletions asset/official-single-page-footer.css
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,9 @@ footer {
padding-top: 30px !important;
text-align: center;
font-size: 20px;
letter-spacing: 2px;
line-height: 1.5em;
font-weight: bold;
height: auto !important;
padding-bottom: 30px !important;
border-bottom: 1px solid #333 !important;
}

footer::after {
content: "O O O O O   O\AO O O O O O O";
white-space: pre;
}
3 changes: 3 additions & 0 deletions lib/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/** @format */
/// <reference types="node" />
import { Command, flags } from "@oclif/command";
declare class Zigzi extends Command {
static description: string;
Expand All @@ -19,6 +20,8 @@ declare class Zigzi extends Command {
marp(): Promise<void>;
toHtml(): Promise<void>;
toPdf(): Promise<void>;
toPdfOfficial(): Promise<void>;
mergePdfBuffers(target1: Buffer, target2: Buffer): Promise<Uint8Array>;
setVSCodeSetting(): Promise<void>;
}
export = Zigzi;
144 changes: 128 additions & 16 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,12 @@ const child_process = require("child_process");
const marked = require("marked");
const puppeteer = require("puppeteer");
const shell = require("shelljs");
const pdfParse = require("pdf-parse");
const opentype = require("opentype.js");
const fm = require("front-matter");
const command_1 = require("@oclif/command");
const fs_1 = require("fs");
const pdf_lib_1 = require("pdf-lib");
const convertCss = {
zb_ppt: __dirname + "/../asset/ppt.css",
zb_ppt_large: __dirname + "/../asset/ppt-large.css",
Expand Down Expand Up @@ -36,35 +40,48 @@ class Zigzi extends command_1.Command {
else {
await this.toHtml();
if (output === "pdf") {
await this.toPdf();
if (flags.template === "zb_official") {
await this.toPdfOfficial();
}
else {
await this.toPdf();
}
}
}
}
}
async marp() {
const { args, flags } = this.parse(Zigzi);
let md = fs.readFileSync(this.file, "utf8");
const cssFile = (md.indexOf("theme: ppt-large") > 0) ? convertCss["zb_ppt_large"] : convertCss["zb_ppt"];
const cssFile = md.indexOf("theme: ppt-large") > 0
? convertCss["zb_ppt_large"]
: convertCss["zb_ppt"];
const pdfFileName = `${this.file.replace(".md", ".pdf")}`;
await child_process.execSync(`npx @marp-team/marp-cli@latest --allow-local-files ${this.file} -o ${pdfFileName} --theme ${cssFile}`);
}
async toHtml() {
const { flags } = this.parse(Zigzi);
const cssType = convertCss[this.converType];
const htmlFileName = `${this.file.replace(".md", ".html")}`;
let md = fs.readFileSync(this.file, "utf8");
const md = fs.readFileSync(this.file, "utf8");
let css = fs.readFileSync(cssType, "utf8");
const pageBreak = /<!-- page-break -->/g;
md = md.replace(pageBreak, `<hr class="page-break" />`);
md = md.replace(RegExp(/<!--[^>]/g), "");
md = md.replace(RegExp(/-->/g), "");
const modifiedMd = md
.replace(pageBreak, `<hr class="page-break" />`)
.replace(RegExp(/<!--[^>]/g), "")
.replace(RegExp(/-->/g), "")
.replace(/^---$.*^---$/ms, "");
let markedHtml;
if (flags.template === "zb_official") {
markedHtml = `<header></header> ${marked(md)}<footer></footer>`;
const { attributes: { 주소, 전화번호 } = { 주소: "", 전화번호: "" }, } = fm(md);
markedHtml = `<header></header> ${marked(modifiedMd)}<footer></footer>`;
css += `header::after{
content: "${주소}\\ATel) ${전화번호}"
}`;
}
else {
markedHtml = marked(md);
markedHtml = marked(modifiedMd);
}
const css = fs.readFileSync(cssType, "utf8");
const html = `
<!doctype html>
<html>
Expand All @@ -84,9 +101,6 @@ class Zigzi extends command_1.Command {
const { flags } = this.parse(Zigzi);
const htmlFileName = `${this.file.replace(".md", ".html")}`;
const pdfFileName = `${this.file.replace(".md", ".pdf")}`;
const headerFooterTemplate = (flags.template === "zb_doc") ? {
displayHeaderFooter: true
} : {};
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto(`file://${path.resolve(htmlFileName)}`, {
Expand All @@ -99,7 +113,7 @@ class Zigzi extends command_1.Command {
printBackground: true,
headerTemplate: `<div class="header" style="background:#ffa400; top:0 !important; width:16px; height:884px; margin:-20px 0 0 !important; padding:0 !important; -webkit-print-color-adjust: exact;"></div>`,
footerTemplate: `<div class="footer" style="padding: 0 !important; margin: 0 !important; z-index:100 !important; position:relative !important; -webkit-print-color-adjust: exact; width: 100%; text-align: center; color:#000; font-size: 10px; font-family: Arial, sans-serif;"><span class="pageNumber"></span></div>`,
displayHeaderFooter: (flags.template === "zb_doc"),
displayHeaderFooter: flags.template === "zb_doc",
margin: {
top: 50,
bottom: 50,
Expand All @@ -110,13 +124,111 @@ class Zigzi extends command_1.Command {
await browser.close();
await fs.unlinkSync(`${htmlFileName}`);
}
async toPdfOfficial() {
const md = fs.readFileSync(this.file, "utf8");
const { attributes: { footer = [] }, } = fm(md);
const htmlFileName = `${this.file.replace(".md", ".html")}`;
const pdfFileName = `${this.file.replace(".md", ".pdf")}`;
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto(`file://${path.resolve(htmlFileName)}`, {
waitUntil: "networkidle2",
timeout: 60000 * 2,
});
await page.emulateMediaType("screen");
const font = opentype.loadSync(path.resolve(__dirname, "../asset/SpoqaHanSansBold.ttf"));
const footerSvg = footer
.map((str) => {
const p = font.getPath(str, 0, 0, 21);
const { x1, x2, y1, y2 } = p.getBoundingBox();
const height = y2 - y1;
const width = x2 - x1;
const svgPath = p.toSVG(2);
return `<svg viewBox="${x1} ${y1} ${width} ${height}" width="${width}" height="${height * 1.75}" xmlns="http://www.w3.org/2000/svg" fill="black">${svgPath}</svg>`;
})
.join("");
const headerTemplate = "<div></div>";
const footerTemplate = `
<style>
.footer {
width: 100% !important;
margin: 0 58px 15px;
text-align: center !important;
padding-top: 30px !important;
padding-bottom: 30px !important;
border-bottom: 1px solid #333 !important;
display: flex;
flex-direction: column;
align-items: center;
}
</style>
<div class="footer">
${footerSvg}
</div>
`;
const baseOpt = {
format: "a4",
path: `${path.resolve(pdfFileName)}`,
headerTemplate,
footerTemplate,
displayHeaderFooter: true,
margin: {
top: 50,
bottom: 50,
left: 58,
right: 58,
},
timeout: 1000 * 60 * 10,
};
await page.pdf(baseOpt);
const dataBuffer = fs.readFileSync(path.resolve(pdfFileName));
const pdfInfo = await pdfParse(dataBuffer);
const numPages = pdfInfo.numpages;
if (numPages === 1) {
await page.addStyleTag({
path: path.resolve(__dirname, "../asset/official-single-page-footer.css"),
});
await page.addStyleTag({
content: `
footer::after {
content: "${footer.join("\\A")}";
white-space: pre;
}`,
});
await page.pdf(Object.assign(Object.assign({}, baseOpt), { displayHeaderFooter: false }));
}
else {
const restBuffer = await page.pdf(Object.assign(Object.assign({}, baseOpt), { displayHeaderFooter: false, pageRanges: `-${numPages - 1}` }));
const lastPdfName = `${pdfFileName}-last.pdf`;
const lastBuffer = await page.pdf(Object.assign(Object.assign({}, baseOpt), { displayHeaderFooter: true, pageRanges: `${numPages}`, path: lastPdfName }));
await fs.unlinkSync(lastPdfName);
const mergedPdfBuffer = await this.mergePdfBuffers(restBuffer, lastBuffer);
await fs.writeFileSync(`${path.resolve(pdfFileName)}`, mergedPdfBuffer);
}
await browser.close();
await fs.unlinkSync(`${htmlFileName}`);
}
async mergePdfBuffers(target1, target2) {
const pdf1 = await pdf_lib_1.PDFDocument.load(target1);
const pdf2 = await pdf_lib_1.PDFDocument.load(target2);
const mergedPdf = await pdf_lib_1.PDFDocument.create();
const copiedPagesA = await mergedPdf.copyPages(pdf1, pdf1.getPageIndices());
copiedPagesA.forEach((page) => mergedPdf.addPage(page));
const copiedPagesB = await mergedPdf.copyPages(pdf2, pdf2.getPageIndices());
copiedPagesB.forEach((page) => mergedPdf.addPage(page));
return await mergedPdf.save();
}
async setVSCodeSetting() {
const location = shell.exec("pwd");
const setting_location = process.platform === "win32" ? "%APPDATA%\/Code\/User\/" : "~/Library/Application Support/Code/User/";
const setting_location = process.platform === "win32"
? "%APPDATA%/Code/User/"
: "~/Library/Application Support/Code/User/";
JSON.stringify;
shell.cd(setting_location);
shell.exec("pwd");
const settingsBuffer = await (0, fs_1.readFileSync)("settings.json", { encoding: "utf8" });
const settingsBuffer = await (0, fs_1.readFileSync)("settings.json", {
encoding: "utf8",
});
let settings = settingsBuffer;
const css = `
"https://zigbang.github.io/zigzi/asset/ppt.css",
Expand Down Expand Up @@ -151,7 +263,7 @@ Zigzi.flags = {
}),
vscode: command_1.flags.boolean({
description: "vscode markdown template setting",
})
}),
};
Zigzi.args = [
{
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "zigzi",
"version": "1.0.6",
"version": "1.0.11",
"author": "ZIGBANG Co., Ltd.",
"keywords": [
"zigbang",
Expand Down
3 changes: 0 additions & 3 deletions sample/official.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,10 @@ _-- 아래 --_
붙임 :
- 붙임 내용

<!-- page-break -->

# test 1

테스트 테스트 테스트 테스트 테스트 테스트 테스트 테스트 테스트 테스트 테스트 테스트 테스트 테스트

<!-- page-break -->

# test 2

Expand Down
Binary file modified sample/official.pdf
Binary file not shown.
25 changes: 16 additions & 9 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,15 +202,15 @@ class Zigzi extends Command {
const footerTemplate = `
<style>
.footer {
width: 100% !important
margin: 0 58px 15px
text-align: center !important
padding-top: 30px !important
padding-bottom: 30px !important
border-bottom: 1px solid #333 !important
display: flex
flex-direction: column
align-items: center
width: 100% !important;
margin: 0 58px 15px;
text-align: center !important;
padding-top: 30px !important;
padding-bottom: 30px !important;
border-bottom: 1px solid #333 !important;
display: flex;
flex-direction: column;
align-items: center;
}
</style>
<div class="footer">
Expand Down Expand Up @@ -244,6 +244,13 @@ class Zigzi extends Command {
"../asset/official-single-page-footer.css"
),
})
await page.addStyleTag({
content: `
footer::after {
content: "${(footer as string[]).join("\\A")}";
white-space: pre;
}`,
})
await page.pdf({
...baseOpt,
displayHeaderFooter: false,
Expand Down