一、OFD文件在線轉換
OFD(Open Financial Document)是一種開放的金融文檔標準,主要用於企業金融報告、財務發票等場景。由於OFD格式並不支持瀏覽器直接渲染,因此需要進行在線轉換。
OFD文件在線轉換的方式有很多種,其中一種比較常見的方案是使用第三方庫進行轉換,比如使用java開發的ofd2html工具。其原理是將OFD文件解析成XML格式,並生成對應的html文件。這樣就可以在瀏覽器中打開OFD文件了。下面是一段示例代碼:
import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileOutputStream; import org.ofdrw.converter.FontLoader; import org.ofdrw.converter.OFD2HtmlConverter; import org.ofdrw.converter.PageInfo; import org.ofdrw.core.OFD; import org.ofdrw.font.FontName; import org.ofdrw.font.FontSet; public class OFD2HTMLDemo { public static void main(String[] args) throws Exception { String srcOFD = "test.ofd"; FontSet fontSet = new FontSet().add(FontName.SimSun); FontLoader fontLoader = new FontLoader().loadFontSet(fontSet); OFD ofd = new OFD(new File(srcOFD).toURI()); //將OFD文件轉換成html PageInfo pageInfo = new OFD2HtmlConverter().setDrawBoundary(false).convert(ofd, fontLoader); ByteArrayOutputStream out = new ByteArrayOutputStream(); pageInfo.html().toStream(out, pageInfo.getMediaSize()); FileOutputStream fos = new FileOutputStream("test.html"); fos.write(out.toByteArray()); fos.close(); } }
二、OFD文件轉PDF在線
OFD文件並不是所有軟件都支持,比如在某些特定的場景下需要將OFD文件轉換成PDF文件。這時可以選擇在線轉換服務。目前市場上有一些比較成熟的轉換服務,比如AliYun和Foxit。這些服務可以通過API方式進行調用,將OFD文件轉換成PDF格式。
下面是通過AliYun進行OFD文件轉換的示例代碼:
//引入相關的包 import java.io.*; import java.net.HttpURLConnection; import java.net.URL; import java.util.Base64; import org.apache.commons.io.FileUtils; //將OFD文件轉換為PDF格式 public String convertOFD2PDF(String srcOFDFilePath) { try { String host = "http://ofdrenderview.market.alicloudapi.com"; String path = "/api/render/ofd2pdf"; String method = "POST"; String appcode = "your_app_code"; String querys = ""; String bodys = Base64.getEncoder().encodeToString(FileUtils.readFileToByteArray(new File(srcOFDFilePath))); URL url = new URL(host + path + querys); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod(method); conn.setRequestProperty("Authorization", "APPCODE " + appcode); conn.setRequestProperty("Content-Type", "application/json; charset=UTF-8"); //讀取返回的數據 BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8")); String line; StringBuilder response = new StringBuilder(); while ((line = reader.readLine()) != null) { response.append(line); } reader.close(); //將返回的PDF文件保存到本地 String destPDFFilePath = "test.pdf"; FileUtils.writeByteArrayToFile(new File(destPDFFilePath), Base64.getDecoder().decode(response.toString())); return destPDFFilePath; } catch (Exception e) { e.printStackTrace(); } return ""; }
三、瀏覽器預覽不了OFD文件
OFD文件並不是瀏覽器原生支持的格式,在線預覽時需要藉助第三方插件或者進行轉換。常見的OFD文件在線瀏覽解決方案有兩種,一種是基於OFD轉成html或者PDF並在瀏覽器中展示,另一種是基於第三方的插件進行展示。
下面是基於第三方插件進行OFD文件在線預覽的示例代碼,需要在HTML頁面中引入OFD瀏覽器插件:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>OFD文件在線預覽</title> </head> <body> <object data="test.ofd" type="application/ofd" width="600" height="800"> <param name="ladpversion" value="7.1.0.0"> <param name="page" value="1"> </object> </body> </html>
四、OFD文件在線預覽前端實現
OFD文件在線預覽也可以通過前端技術實現,支持更加靈活的用戶交互方式。通常的做法是通過JavaScript將OFD文件解析成對應的HTML和CSS,然後在瀏覽器中展示。OFD.js是一個比較成熟的OFD解析庫,可以方便地實現OFD文件的在線預覽。
下面是通過OFD.js實現OFD文件在線預覽的示例代碼:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>OFD文件在線預覽</title> <!--引入CSS--> <link rel="stylesheet" href="OFDJSViewer/OFDJSViewer.css"> <!--引入JavaScript--> <script src="OFDJSViewer/OFDJSViewer.min.js"></script> </head> <body> <div id="ofd_viewer"></div> <script> var viewer = new OFDJSViewer({ container: document.getElementById("ofd_viewer"), ofdFile: "test.ofd", toolbar: true, toolbarPosition: OFDJSViewer.TOOLBAR_POSITION_TOP }); viewer.render(); </script> </body> </html>
五、結語
OFD文件在線預覽涉及的技術包括文件格式轉換、API調用、第三方插件、JavaScript等多個方面。開發人員可以根據具體情況選擇不同方案進行實現,使用戶能夠方便地在線預覽OFD文件。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/185647.html