Font converter | API Solution for Javascript
Contents
[
Hide
Show
]Overview
The Aspose.Font for JavaScript library is a solution that allows you to convert fonts between popular supported formats.
How to convert the font into the desired format?
- Create a ‘FileReader’.
- Call AsposeFontConvert function.
- The resulting name file is set, in
json.fileNameResult
. - Next, if the
json.errorCode
is 0, then you can get links to result files. If thejson.errorCode
parameter is not equal to 0 and, accordingly, there will be an error in your file, then information about such an error will be contained in thejson.errorText
. - As a result, the DownloadFile function generates a link and allows you to download the resulting file to the user’s operating system.
Below is the sample which converts font from
TTF
toWOFF
format.
1 var fTTF2WOFF = function (e) {
2 const file_reader = new FileReader();
3 file_reader.onload = (event) => {
4 const json = AsposeFontConvert(event.target.result, e.target.files[0].name, Module.FontType.TTF, Module.FontSavingFormats.WOFF);
5 if (json.errorCode == 0) document.getElementById('output').textContent = json.fileNameResult;
6 else document.getElementById('output').textContent = json.errorText;
7 DownloadFile(json.fileNameResult, "woff");
8 }
9 file_reader.readAsArrayBuffer(e.target.files[0]);
10 }
or using Web Worker:
1 /*Create Web Worker*/
2 const AsposeFontWebWorker = new Worker("AsposeFontforJS.js");
3 AsposeFontWebWorker.onerror = evt => console.log(`Error from Web Worker: ${evt.message}`);
4 AsposeFontWebWorker.onmessage = evt => document.getElementById('output').textContent =
5 (evt.data == 'ready') ? 'library loaded!' :
6 (evt.data.json.errorCode == 0) ? `Result:\n${DownloadFile(evt.data.json.fileNameResult, "font/ttf", evt.data.params[0])}` : `Error: ${evt.data.json.errorText}`;
7
8 /*Event handler*/
9 const fOTFtoTTF = e => {
10 const file_reader = new FileReader();
11 file_reader.onload = event => {
12 /*Convert a OTF fonts to TTF and save - Ask Web Worker*/
13 AsposeFontWebWorker.postMessage({ "operation": 'AsposeFontConvert', "params": [event.target.result, e.target.files[0].name, 'Module.FontType.OTF', 'Module.FontSavingFormats.TTF'] }, [event.target.result]);
14 };
15 file_reader.readAsArrayBuffer(e.target.files[0]);
16 };
17
18 /*Make a link to download the result file*/
19 const DownloadFile = function (filename, mime, content) {
20 mime = mime || "application/octet-stream";
21 var link = document.createElement("a");
22 link.href = URL.createObjectURL(new Blob([content], {type: mime}));
23 link.download = filename;
24 link.textContent = filename;
25 link.title = "Click here to download the file";
26 document.getElementById('fileDownload').appendChild(link);
27 document.getElementById('fileDownload').appendChild(document.createElement("br"));
28 }
How to get the solution?
If you want to get the library, go to Aspose.Font for JavaScript product page. There you will find more opportunities that the solution gives you. You can download a free trial from or buy the product there.
If you have any issues or questions, feel free to post them at the Aspose.Font.Product Family section of the Free Support Forum and within a few hours our support team will clear everything up for you.