將 PFB 轉換為 TTF 在 JavaScript 中

PFBTTF 是兩種常見的字型格式,用於數位設計。PFB,或 PostScript 字型二進位,是一種較舊的格式,而 TTF,或 TrueType 字型,則更為現代且廣泛支持。如果你有一個 PFB 字型,並需要在需要 TTF 的項目中使用它,你將需要進行轉換。在本指南中,我們將指導你如何使用 JavaScript 將 PFB 轉換為 TTF。

本文涵蓋了以下主題:

JavaScript 庫將 PFB 轉換為 TTF

我們將使用 Aspose.Font for JavaScript 庫來進行 PFB 到 TTF 的轉換。這是一個強大的庫,可以簡化將 PFB 字型轉換為 TTF 格式的工作。使用 Aspose.Font,你可以輕鬆地將字型轉換功能集成到你的 JavaScript 應用程序中,確保與廣泛的平台和設備兼容。它使開發人員能夠以多種格式操作、提取和管理字型數據。

下載該庫 並按照 安裝 Aspose.Font for JavaScript 的說明進行操作。

使用 JavaScript 將 PFB 轉換為 TTF

我們可以在 JavaScript 的 Web 應用程序中快速將 PFB 字型轉換為 TTF 格式。我們將加載 PFB 文件,然後將其轉換為 TTF。為了避免阻塞主 UI 線程,將使用 Web 工作線程進行繁重的處理。這使得我們的轉換應用程序更具用戶友好性,簡化了轉換過程,並實現了高效的文件下載。

請按照以下步驟使用 JavaScript 將 PFB 字型轉換為 TrueType 字型:

  • 使用以下代碼片段創建一個 Web 工作線程:
/*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"));
}
  • 通過以下步驟將 PFB 轉換為 TTF:
  1. 選擇要轉換的 PFB 文件。
  2. 創建一個 FileReader 對象。
  3. 執行 AsposeFontConvertToSVG 函數。
  4. json.fileNameResult 中設置輸出文件的名稱。
  5. 檢查 json.errorCode 是否為 0。如果是,則獲取轉換後文件的鏈接。如果不是,錯誤詳細信息在 json.errorText 中。
  6. DownloadFile 函數創建一個鏈接,方便你下載轉換後的文件到你的電腦。

以下代碼示例顯示了 如何使用 JavaScript 將 PFB 字型文件轉換為 TTF 格式

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]);
}
將 PFB 轉換為 TTF 在 JavaScript 中

嘗試 PFB 到 TTF 免費在線轉換器

你也可以使用這個免費的在線 PFB 到 TTF 轉換器 工具進行 PFB 文件到 TTF 的轉換。你可以在任何設備上使用任何可用的網頁瀏覽器訪問它。

獲取 JavaScript 字型庫

你可以 獲取免費的臨時許可證,以在沒有評估限制的情況下試用 JavaScript 字型庫。

PFB 到 TTF - 免費資源

除了將 PFB 字型轉換為 TrueType 字型,還可以使用以下資源探索該庫的各種其他功能:

結論

在本文中,我們了解了如何使用 JavaScript 將 PFB 字型轉換為 TTF 格式。我們探索了 Aspose.Font for JavaScript 的使用,它提供了一個強大而高效的解決方案來轉換字型。你也可以使用在線工具進行快速轉換。按照這些步驟,你可以輕鬆地確保與更廣泛的應用程序和設備兼容。如果你有任何問題,請隨時在我們的 免費支持論壇 中聯繫我們。

參見