AsposeFontConvertToTTF | API Solution for Javascript
Contents
[
Hide
Show
]How to convert the font into TTF format?
- Create a ‘FileReader’.
- Call the AsposeFontConvertToTTF function.
- The name of the resulting file is set, in
json.fileNameResult
. - Next, if the
json.errorCode
is 0, then you can get links to the 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 EOT
to TTF
format.
1 var fEOT2TTF = function (e) {
2 const file_reader = new FileReader();
3 file_reader.onload = (event) => {
4 const json = AsposeFontConvertToTTF(event.target.result, e.target.files[0].name, Module.FontType.OTF);
5 if (json.errorCode == 0) document.getElementById('output').textContent = json.fileNameResult;
6 else document.getElementById('output').textContent = json.errorText;
7 DownloadFile(json.fileNameResult, "font/ttf");
8 }
9 file_reader.readAsArrayBuffer(e.target.files[0]);
10 }
Web Worker example:
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 fEOTtoTTF = 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": 'AsposeFontConvertToTTF', "params": [event.target.result, e.target.files[0].name, 'Module.FontType.OTF'] }, [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 }
See Also
- function AsposeFontConvert
- enum FontType