Skip to content

Instantly share code, notes, and snippets.

@aspose-com-gists
Created September 19, 2024 05:26
Show Gist options
  • Save aspose-com-gists/c6c8fb61b3121d59978c8fdd52202ffa to your computer and use it in GitHub Desktop.
Save aspose-com-gists/c6c8fb61b3121d59978c8fdd52202ffa to your computer and use it in GitHub Desktop.
Convert PFB to TTF in JavaScript
var fPFB2TTF = function (e) {
const file_reader = new FileReader();
file_reader.onload = (event) => {
const json = AsposeFontConvertToTTF(event.target.result, e.target.files[0].name, Module.FontType.Type1, Module.FontSavingFormats.TTF);
if (json.errorCode == 0) document.getElementById('output').textContent = json.fileNameResult;
else document.getElementById('output').textContent = json.errorText;
DownloadFile(json.fileNameResult);
}
file_reader.readAsArrayBuffer(e.target.files[0]);
}
/*Create Web Worker*/
const AsposeFontWebWorker = new Worker("AsposeFontforJS.js");
AsposeFontWebWorker.onerror = evt => console.log(`Error from Web Worker: ${evt.message}`);
AsposeFontWebWorker.onmessage = evt => document.getElementById('output').textContent =
(evt.data == 'ready') ? 'library loaded!' :
(evt.data.json.errorCode == 0) ? `Result:\n${DownloadFile(evt.data.json.fileNameResult, "font/ttf", evt.data.params[0])}` : `Error: ${evt.data.json.errorText}`;
/*Event handler*/
const fPFBtoTTF = e => {
const file_reader = new FileReader();
file_reader.onload = event => {
/*Convert a PFB fonts to TTF and save - Ask Web Worker*/
AsposeFontWebWorker.postMessage({ "operation": 'AsposeFontConvertToTTF', "params": [event.target.result, e.target.files[0].name, 'Module.FontType.OTF'] }, [event.target.result]);
};
file_reader.readAsArrayBuffer(e.target.files[0]);
};
/*Make a link to download the result file*/
const DownloadFile = function (filename, mime, content) {
mime = mime || "application/octet-stream";
var link = document.createElement("a");
link.href = URL.createObjectURL(new Blob([content], {type: mime}));
link.download = filename;
link.textContent = filename;
link.title = "Click here to download the file";
document.getElementById('fileDownload').appendChild(link);
document.getElementById('fileDownload').appendChild(document.createElement("br"));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment