AsposeFontConvertToWOFF2 | API Solution for Javascript

How to convert the font into WOFF2 format?

  1. Create a ‘FileReader’.
  2. Call the AsposeFontConvertToWOFF2 function.
  3. The name of the resulting file is set, in json.fileNameResult.
  4. Next, if the json.errorCode is 0, then you can get links to the result files. If the json.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 in the json.errorText.
  5. The DownloadFile function generates a link and lets you download the resulting file to the user’s operating system.

Below is the code example for the EOT to WOFF2 conversion.

 1  var fEOT2WOFF2 = function (e) {
 2    const file_reader = new FileReader();
 3    file_reader.onload = (event) => {
 4      const json = AsposeFontConvertToWOFF2(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 fEOTtoWOFF2 = 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": 'AsposeFontConvertToWOFF2', "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  }

Have any questions about Aspose.Font?



Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.