Skip to content

Commit

Permalink
pdf tools moved into pool and reorg
Browse files Browse the repository at this point in the history
  • Loading branch information
btopro committed Jun 16, 2022
1 parent f1a5d9a commit fe2d1b4
Show file tree
Hide file tree
Showing 18 changed files with 840 additions and 247 deletions.
20 changes: 9 additions & 11 deletions api/apps/haxcms/site/epub.js → api/apps/haxcms/siteToEpub.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import { stdPostBody, stdResponse } from "../../utilities/requestHelpers.js";
import { JSONOutlineSchema } from "./lib/JSONOutlineSchema.js";

import url from "url";
import Epub from "epub-gen";
import { JSONOutlineSchema } from "../lib/JSONOutlineSchema.js";

// site object to validate response from passed in url
export default async function handler(req, res) {
let content = '';
const body = JSON.parse(req.body);
const body = stdPostBody(req);
// get URL bits for validating and forming calls
const parseURL = url.parse(body.url.replace('/site.json',''));
// verify we have a path / host
Expand All @@ -25,16 +28,11 @@ export default async function handler(req, res) {
new Epub(options).promise.then(() => console.log('Done'));
}
if (!body.cacheBuster) {
res.setHeader('Cache-Control', 'max-age=0, s-maxage=86400');
res = stdResponse(res, content, { cache: 86400 });
}
else {
res = stdResponse(res, content);
}
res.setHeader("Access-Control-Allow-Credentials", "true");
res.setHeader("Access-Control-Allow-Origin", "*");
res.setHeader("Access-Control-Allow-Methods", "OPTIONS,POST");
res.setHeader("Access-Control-Allow-Headers", "X-CSRF-Token, X-Requested-With, Accept, Accept-Version, Content-Length, Content-MD5, Content-Type, Date, X-Api-Version");
res.json({
status: "success",
data: content
});
}

export async function pagesAsData(site) {
Expand Down
13 changes: 8 additions & 5 deletions api/apps/haxcms/site/html.js → api/apps/haxcms/siteToHtml.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
import url from "url";
import { JSONOutlineSchema } from "../lib/JSONOutlineSchema.js";
import { stdPostBody, stdResponse, invalidRequest } from "../../../utilities/requestHelpers.js";
import { stdPostBody, stdResponse } from "../../utilities/requestHelpers.js";
import { JSONOutlineSchema } from "./lib/JSONOutlineSchema.js";

// site object to validate response from passed in url
export default async function handler(req, res) {
let content = '';
// use this if POST data is what's being sent
const body = stdPostBody(req);
// get URL bits for validating and forming calls
const parseURL = url.parse(body.url.replace('/site.json',''));
parseURL.endsWith('/')
let url = body.url.replace('/site.json','');
// handle trailing slash
if (url.endsWith('/')) {
url = url.slice(0, -1);
}
var parseURL = new URL(url);
// verify we have a path / host
if (parseURL.pathname && parseURL.host) {
const base = `${parseURL.protocol}//${parseURL.host}${parseURL.pathname}`;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
import { fetch, stdPostBody, stdResponse } from "../utilities/requestHelpers.js";

// @todo remote load HTML or accept string
// leverage the WC registry supplied OR default to a CDN
// return hydrated shadow using Lit hydration thing
import fetch from "node-fetch";
import { render } from '@lit-labs/ssr/lib/render-with-global-dom-shim.js';
import { Readable } from 'stream';
import { html as htmlLit } from 'lit';
import { html } from 'lit';
import { unsafeHTML } from 'lit-html/directives/unsafe-html.js';
import pkg from 'jsdom';
const { JSDOM } = pkg;

import "@lrnwebcomponents/haxcms-elements/lib/core/haxcms-site-builder.js";
export default async function handler(req, res) {
const body = JSON.parse(req.body);
var html = body.html;
const body = stdPostBody(req);
var htmlContent = body.html;
var domBody = '';
// md is actually a link reference so fetch it 1st
if (body.type === 'link' && html) {
html = await fetch(html.trim()).then((d) => d.ok ? d.text(): '');
const dom = new JSDOM(html);
if (body.type === 'link' && htmlContent) {
htmlContent = await fetch(htmlContent.trim()).then((d) => d.ok ? d.text(): '');
const dom = new JSDOM(htmlContent);
domBody = dom.window.document.querySelector("body").innerHTML;
console.log(domBody);
}
const ssrContent = await readStream(Readable.from(render(htmlLit`
const ssrContent = await readStream(Readable.from(render(html`
${unsafeHTML(domBody)}
<script type="module">
// Hydrate template-shadowroots eagerly after rendering (for browsers without
Expand All @@ -35,18 +35,10 @@ export default async function handler(req, res) {
}
// ...
// Load and hydrate components lazily
import("@lrnwebcomponents/wc-autoload/wc-autoload.js");
import("./node_modules/@lrnwebcomponents/a11y-gif-player/a11y-gif-player.js");
</script>
`)));

res.setHeader("Access-Control-Allow-Credentials", "true");
res.setHeader("Access-Control-Allow-Origin", "*");
res.setHeader("Access-Control-Allow-Methods", "GET,OPTIONS,PATCH,DELETE,POST,PUT");
res.setHeader("Access-Control-Allow-Headers", "X-CSRF-Token, X-Requested-With, Accept, Accept-Version, Content-Length, Content-MD5, Content-Type, Date, X-Api-Version");
res.json({
status: "success",
data: ssrContent
});
res = stdResponse(res, ssrContent)
}

function readStream(stream, encoding = "utf8") {
Expand Down
12 changes: 11 additions & 1 deletion api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,16 @@
"stream": "0.0.2",
"uuid": "8.3.2",
"node-fetch": "3.2.4",
"epub-gen": "0.1.0"
"epub-gen": "0.1.0",
"mammoth": "1.4.21",
"busboy": "1.5.0",
"puppeteer-core": "13.5.2",
"chrome-aws-lambda": "10.1.0",
"asciify-image": "0.1.8",
"concat-stream": "2.0.0",
"html-pdf": "3.0.1"
},
"peerDependencies": {
"puppeteer": "13.5.2"
}
}
72 changes: 72 additions & 0 deletions api/services/media/format/docxToHtml.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import { stdResponse } from "../../../utilities/requestHelpers.js";
import { getBrowserInstance } from '../../../utilities/getBrowserInstance.js';

import df from 'mammoth';
const { convertToHtml } = df;
import busboy from 'busboy';
import concat from "concat-stream";

export default async function handler(req, res) {
var html = '';
var img = '';
var buffer = {
filename: null,
data: null,
};
const bb = busboy({ headers: req.headers });
bb.on('file', async (name, file, info) => {
const { filename, encoding, mimeType } = info;
if(filename.length > 0 && mimeType == "application/vnd.openxmlformats-officedocument.wordprocessingml.document") {
file.pipe(concat((fileBuffer) => {
buffer.filename = filename;
buffer.data = fileBuffer;
}));
}
});
// file closed / finished
bb.on('close', async () => {
if (buffer.data) {
try {
html = await convertToHtml({buffer: buffer.data})
.then((result) => {
return result.value; // The generated HTML
});
// generate a screenshot of the HTML blob
// capture options
var screenshotOptions = {
quality: 75,
type: 'jpeg',
encoding: "base64"
};
let browser = null
try {
browser = await getBrowserInstance();
let page = await browser.newPage();
await page.setContent(html)
img = await page.screenshot(screenshotOptions)
} catch (error) {
console.log(error)
} finally {
if (browser !== null) {
await browser.close()
}
}
}
catch(e) {
// put in the output
html = e;
}
}
res = stdResponse(res,
{
contents: html,
filename: buffer.filename,
image: img
},
{
methods: "GET,OPTIONS,PATCH,DELETE,POST,PUT"
}
);
});
req.pipe(bb);
}
76 changes: 76 additions & 0 deletions api/services/media/format/docxToPdf.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import { stdResponse } from "../../../utilities/requestHelpers.js";
import df from 'mammoth';
const { convertToHtml } = df;
import busboy from 'busboy';
import concat from "concat-stream";
import {create as toPDF } from 'html-pdf';

const generatePDF = (html) => {
return new Promise (
(resolve, reject) => {
toPDF(html, {
format: 'A4',
orientation: 'portrait',
margin: {
top: "10mm",
right: "10mm",
bottom: "10mm",
left: "10mm",
},
timeout: 30000,
}).toBuffer((error, buffer) => {
if(error) {
reject(error)
}
else {
resolve(buffer.toString('base64'))
}
});
}
)
}

export default async function handler(req, res) {
var string64 = '';
var buffer = {
filename: null,
data: null,
};

const bb = busboy({ headers: req.headers });
bb.on('file', async (name, file, info) => {
const { filename, encoding, mimeType } = info;
if(filename.length > 0 && mimeType == "application/vnd.openxmlformats-officedocument.wordprocessingml.document") {
file.pipe(concat((fileBuffer) => {
buffer.filename = filename;
buffer.data = fileBuffer;
}));
}
});
// file closed / finished
bb.on('close', async () => {
if (buffer.data) {
try {
string64 = await convertToHtml({buffer: buffer.data})
.then(async (result) => {
let html = `<html><body>${result.value}</body></html>`;
return await generatePDF(html);
});
}
catch(e) {
// put in the output
string64 = e;
}
}

res = stdResponse(res, {
filename: buffer.filename,
pdf: string64
},
{
methods: "GET,OPTIONS,PATCH,DELETE,POST,PUT"
}
);
});
req.pipe(bb);
}
60 changes: 60 additions & 0 deletions api/services/media/format/imgToAscii.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { stdResponse } from "../../../utilities/requestHelpers.js";

import asciify from 'asciify-image';
import busboy from 'busboy';
import concat from "concat-stream";

export default async function handler(req, res) {
var img = '';
var buffer = {
filename: null,
data: null,
};
const bb = busboy({ headers: req.headers });
bb.on('file', async (name, file, info) => {
const { filename, encoding, mimeType } = info;
// ensure we have a filename
if(filename.length > 0 && mimeType.startsWith("image/") === true) {
file.pipe(concat((fileBuffer) => {
buffer.filename = filename;
buffer.data = fileBuffer;
}));
}
});
// file closed / finished
bb.on('close', async () => {
if (buffer.data) {
try {
var options = {
fit: 'box',
width: 50,
height: 25
}
img = await asciify(buffer.data, options)
.then(function (asciified) {
// Print asciified image to console
console.log(asciified);
return asciified;
})
.catch(function (err) {
// Print error to console
console.error(err);
});
}
catch(e) {
// put in the output
img = e;
}
}
res = stdResponse(res,
{
filename: buffer.filename,
image: img
},
{
methods: "GET,OPTIONS,PATCH,DELETE,POST,PUT"
}
);
});
req.pipe(bb);
}
File renamed without changes.

1 comment on commit fe2d1b4

@vercel
Copy link

@vercel vercel bot commented on fe2d1b4 Jun 16, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

lrnwebcomponents – ./

lrnwebcomponents-elmsln.vercel.app
lrnwebcomponents.vercel.app
lrnwebcomponents-git-master-elmsln.vercel.app

Please sign in to comment.