整合營銷服務(wù)商

          電腦端+手機(jī)端+微信端=數(shù)據(jù)同步管理

          免費(fèi)咨詢熱線:

          怎么把PDF轉(zhuǎn)換成HTML?這幾個(gè)工具幫你輕松解決

          于程序員來說,每天編寫代碼是他們必做的事。就像從事網(wǎng)課教育行業(yè)的小伙伴來說,他們需要在代碼的框架里加入PDF文檔的講義內(nèi)容。如果采取手動(dòng)撰寫PDF里的內(nèi)容會(huì)拉低整體的工作效率,但是直接添加將PDF轉(zhuǎn)換為HTML格式用到程序框架中的話,就可以省掉許多無謂步驟了。那你們知道PDF轉(zhuǎn)HTML格式怎么弄?下面我就來告訴你們幾個(gè)簡單的轉(zhuǎn)換方法,你們快來看看吧!

          方法一:使用全能PDF轉(zhuǎn)換助手

          這是一款專業(yè)的文件轉(zhuǎn)換軟件,它能支持很多種文件轉(zhuǎn)換格式,包括PDF轉(zhuǎn)HTML、PDF轉(zhuǎn)Word、PDF轉(zhuǎn)Excel、PDF轉(zhuǎn)PPT、WPS轉(zhuǎn)Word、CAD轉(zhuǎn)圖片等格式,而且它大部分的轉(zhuǎn)換功能都支持批量的文件導(dǎo)入,對(duì)于辦公人士來說,非常方便。

          操作如下:

          第一步:首先在電腦上打開該軟件,點(diǎn)擊【PDF轉(zhuǎn)其他】,選擇【PDF轉(zhuǎn)HTML】,導(dǎo)入一個(gè)或者多個(gè)需要轉(zhuǎn)換的PDF文件。

          第二步:根據(jù)我們的需要,在文件后面選擇要轉(zhuǎn)換的頁碼,再點(diǎn)擊右下角的【開始轉(zhuǎn)換】,稍等片刻,系統(tǒng)就會(huì)自動(dòng)轉(zhuǎn)換并下載好HTML格式的文件了。

          另外這款軟件還新推出了手機(jī)APP版本,可以隨時(shí)幫助我們進(jìn)行轉(zhuǎn)換文件格式、翻譯、壓縮等操作,如果你們有興趣的話,也可以去下載試一試。

          方法二:使用萬能文字識(shí)別軟件

          這個(gè)軟件雖然是一個(gè)識(shí)別軟件,但它的功能也不少。該軟件也支持PDF轉(zhuǎn)換處理,我們只需使用這個(gè)軟件就可以快速地把PDF轉(zhuǎn)換HTML格式。

          操作如下:

          第一步:首先依次點(diǎn)擊該軟件的【PDF轉(zhuǎn)換處理】——【PDF轉(zhuǎn)HTML】,然后導(dǎo)入需要轉(zhuǎn)換格式的PDF文件。

          第二步:然后根據(jù)我們的需要點(diǎn)擊下面的【添加文件】添加多個(gè)需要轉(zhuǎn)換的PDF文件,再點(diǎn)擊【開始轉(zhuǎn)換】,HTML的格式就轉(zhuǎn)換好了。

          以上就是今天的內(nèi)容,現(xiàn)在你們應(yīng)該都知道PDF轉(zhuǎn)HTML格式怎么弄了吧?如果大家還知道其他更好的方法,歡迎在評(píng)論區(qū)留言哦。

          里講一種實(shí)現(xiàn)起來比較簡單的html轉(zhuǎn)pdf下載的實(shí)現(xiàn)。

          依賴插件

          html2canvas jspdf

          思路

          通過html2canvas,我們可以將指定的一個(gè)dom元素,渲染到canvas中,然后從canva中獲得該圖片,并將圖片通過jspdf來生成。

          代碼

          function createPdf (selector,pagesize,direction,title){
              var key = pagesize +''+direction;
              var settings = {
                  '00' : {
                      pdf : {orientation : 'portrait',format : 'a4',unit : 'px'},
                      width : 448,
                      height : 632.5
                  },
                  '01' : {
                      pdf : {orientation : 'landscape',format : 'a4',unit : 'px'},
                      width : 632.5,
                      height : 448
                  },
                  '10' : {
                      pdf : {orientation : 'portrait',format : 'a3',unit : 'px'},
                      width : 632.5,
                      height : 894.2
                  },
                  '11' : {
                      pdf : {orientation : 'landscape',format : 'a3',unit : 'px'},
                      width : 894.2,
                      height : 632.5
                  }
              };
              var set = settings[key];
              var doc = new jsPDF(set.pdf);
              var arr = [];//根據(jù)順序保存
              var $arr = $(selector);
              function tempCreate(){
                  if($arr.length == 0){//沒有啦
                      //執(zhí)行生成
                      tempPdf();
                  }else{
                      var $dom = $arr.splice(0,1);
                      html2canvas($dom[0]).then(canvas => {
                          var dataurl = canvas.toDataURL('image/png');
                          arr.push(dataurl);
                          tempCreate();
                      });
                  }
              }
              function tempPdf(){
                  arr.forEach((item,i)=>{
                      if(i !== 0){
                          doc.addPage();
                      }
                      doc.addImage(item,'png',-1,-1,set.width,set.height);//根據(jù)不同的寬高寫入
                  })
                  //根據(jù)當(dāng)前的作業(yè)名稱
                  doc.save(title+'.pdf');
              }
              tempCreate();
          }

          需要指定容器(依賴jquery),然后指定紙張A4或 A3,以及橫縱向。

          //調(diào)用
          createPdf('.single-page',0,0,'test')

          當(dāng)然,如果是數(shù)據(jù)量很大的話,就不建議在前臺(tái)生成了,最好還是放在后端去做。個(gè)人測試過,做A4的圖片生成PDF,當(dāng)數(shù)量大約在100左右的時(shí)候,瀏覽器就崩潰了,如果只是幾頁的數(shù)據(jù)的話,這個(gè)方式還是很方便的。

          Ps:瀏覽器要是現(xiàn)代瀏覽器哈。

          參考資料

          html2canvas : http://html2canvas.hertzen.com/ jspdf :https://github.com/MrRio/jsPDF

          多編程技術(shù)文章,請查閱IOKKS - 專業(yè)編程技術(shù)分享平臺(tái)

          在本文中,我們將確保讀取我們放在指定文件夾中的HTML文件,并解析其中的變量并替換為它們的實(shí)際值。然后,我使用了"openhtmltopdf-pdfbox"庫修改了HTML文件。我們將介紹如何將其轉(zhuǎn)換為PDF文件。

          首先,我們將讀取我們確定的文件夾下的HTML文件,解析它,并將我們自己的動(dòng)態(tài)值傳遞給HTML中相關(guān)的變量。我們將使用"openhtmltopdf-pdfbox"庫中最新更新的形式將HTML文件轉(zhuǎn)換為PDF文件。

          我希望這將成為需要這方面幫助的人的參考。您可以在您的Java項(xiàng)目中輕松進(jìn)行轉(zhuǎn)換。您可以在下面看到一個(gè)示例項(xiàng)目。

          首先,我們將創(chuàng)建一個(gè)新的輸入文件夾,我們將在其中讀取我們的輸入HTML文件,并創(chuàng)建一個(gè)輸出文件夾,我們將在其中寫入PDF文件。

          我們可以將HTML文件放在輸入文件夾下。我們定義一個(gè)要在HTML文件中替換的鍵值。這個(gè)鍵值被給定為#NAME#作為示例。在Java中,您可以選擇用外部發(fā)送的值替換您想要的鍵值。

          input folder :  \ConvertHtmlToPDF\input
          
          output folder:  \ConvertHtmlToPDF\output

          <?xml version='1.0' encoding='UTF-8' ?>
          <!DOCTYPE html>
          <html lang="tr">
            <head>
              <meta data-fr-http-equiv="Content-Type" content="text/html; charset=UTF-8">
              </meta>
              <title>Convert to Html to Pdf</title>
              <style type="text/css">
                body {
                  font-family: "Times New Roman", Times, serif;
                  font-size: 40px;
                }
              </style>
            </head>
            <body topmargin="0" leftmargin="0" rightmargin="0" bottommargin="0">
              <table width="700" border="0" cellspacing="0" cellpadding="0" style="background-color: #1859AB;
          
                                           color: white;
                                           font-size: 14px;
                                           border-radius: 1px;
                                           line-height: 1em; height: 30px;">
                <tbody>
                  <tr>
                    <td>
                      <strong style="color:#F8CD00;">   Hello </strong>#NAME#
                    </td>
                  </tr>
                </tbody>
              </table>
            </body>
          </html>

          創(chuàng)建一個(gè)新項(xiàng)目

          我們正在創(chuàng)建一個(gè)新的Spring項(xiàng)目。我正在使用Intellj Idea。

          控制器

          為了在HTML中用值替換鍵,我們將從外部發(fā)送值。我們?yōu)榇司帉懸粋€(gè)rest服務(wù)。

          我們在Controller文件夾下創(chuàng)建"ConvertHtmlToPdfController.java"類。我們在Controller類中創(chuàng)建一個(gè)名為"convertHtmlToPdf"的get方法。我們可以動(dòng)態(tài)地將值傳遞給這個(gè)方法,如下所示。

          package com.works.controller;
          
          import com.works.service.ConvertHtmlToPdfService;
          import org.springframework.http.ResponseEntity;
          import org.springframework.web.bind.annotation.GetMapping;
          import org.springframework.web.bind.annotation.PathVariable;
          import org.springframework.web.bind.annotation.RequestBody;
          import org.springframework.web.bind.annotation.RequestMapping;
          import org.springframework.web.bind.annotation.RestController;
          
          @RestController
          @RequestMapping("")
          public class ConvertHtmlToPdfController {
          
              private final ConvertHtmlToPdfService convertHtmlToPdfService;
          
              public ConvertHtmlToPdfController(ConvertHtmlToPdfService convertHtmlToPdfService) {
                  this.convertHtmlToPdfService = convertHtmlToPdfService;
              }
          
              @GetMapping("/convertHtmlToPdf/{variableValue}")
              public ResponseEntity<String> convertHtmlToPdf(@PathVariable @RequestBody String variableValue) {
                  try {
                      return ResponseEntity.ok(convertHtmlToPdfService.convertHtmlToPdf(variableValue));
                  } catch (Exception e) {
                      throw new RuntimeException(e);
                  }
              }
          
          }

          服務(wù)

          ConvertHtmlToPdfService.java服務(wù)包含一個(gè)名為convertHtmlToPdf的方法。convertHtmlToPdf方法接受字符串variableValue輸入。

          在convertHtmlToPdf服務(wù)方法中;

          "inputFile"變量被定義為讀取輸入文件夾下的html文件。我們可以給這個(gè)變量賦予我們將要讀取的輸入html文件的URL。

          "outputFile"變量被定義為將pdf文件分配給輸出文件夾。我們可以將輸出文件夾的URL賦給這個(gè)變量。

          您還可以從外部讀取字體文件。您可以從輸入文件夾下獲取這個(gè)文件。我們還可以將字體文件所在的URL分配給"fontFile"變量。

          在上述代碼行中,將包含輸入的文件夾的URL傳遞給"ConvertHtmlToPdfUtil.readFileAsString"方法,以讀取輸入文件夾中的HTML文件。

                       String htmlContent = ConvertHtmlToPdfUtil.readFileAsString(inputFile);
          package com.works.util;
          
          import com.openhtmltopdf.pdfboxout.PdfRendererBuilder;
          
          import java.io.BufferedReader;
          import java.io.File;
          import java.io.FileInputStream;
          import java.io.FileOutputStream;
          import java.io.IOException;
          import java.io.InputStreamReader;
          import java.io.OutputStream;
          
          public class ConvertHtmlToPdfUtil {
          
              public static void safeCloseBufferedReader(BufferedReader bufferedReader) throws Exception {
                  try {
                      if (bufferedReader != null) {
                          bufferedReader.close();
                      }
                  } catch (IOException e) {
                      throw new Exception("safeCloseBufferedReader  - the method got an error. " + e.getMessage());
                  }
              }
          
              public static String readFileAsString(String filePath) throws Exception {
                  BufferedReader br = null;
                  String encoding = "UTF-8";
          
                  try {
          
                      br = new BufferedReader(new InputStreamReader(new FileInputStream(filePath), encoding));
          
                      StringBuilder fileContentBuilder = new StringBuilder();
                      String line;
          
                      while ((line = br.readLine()) != null) {
                          if (fileContentBuilder.length() > 0) {
                              fileContentBuilder.append(System.getProperty("line.separator"));
                          }
                          fileContentBuilder.append(line);
                      }
          
                      return fileContentBuilder.toString();
          
                  } catch (Exception e) {
                      new Exception("readFileAsString - the method got an error." + e.getMessage(), e);
                      return null;
                  } finally {
                      safeCloseBufferedReader(br);
                  }
              }
          
              public static OutputStream htmlConvertToPdf(String html, String filePath, String fonts) throws Exception {
                  OutputStream os = null;
                  try {
                      os = new FileOutputStream(filePath);
                      final PdfRendererBuilder pdfBuilder = new PdfRendererBuilder();
                      pdfBuilder.useFastMode();
                      pdfBuilder.withHtmlContent(html, null);
                      String fontPath = fonts;
                      pdfBuilder.useFont(new File(concatPath(fontPath, "times.ttf")), "Times", null, null, false);
                      pdfBuilder.toStream(os);
                      pdfBuilder.run();
                      os.close();
                  } catch (Exception e) {
                      throw new Exception(e.getMessage(), e);
                  } finally {
                      try {
                          if (os != null) {
                              os.close();
                          }
                      } catch (IOException e) {
                      }
                  }
                  return os;
              }
          
              public static String concatPath(String path, String... subPathArr) {
                  for (String subPath : subPathArr) {
                      if (!path.endsWith(File.separator)) {
                          path += File.separator;
                      }
                      path += subPath;
                  }
          
                  return path;
              }
          }

          以上是ConvertHtmlToPdfUtil.readFileAsString方法中的代碼。它使用FileInputStream將HTML文件讀取為字符集,并使用InputStreamReader將其轉(zhuǎn)換為內(nèi)部緩沖區(qū),并使用BufferedReader將其放入內(nèi)部緩沖區(qū)。

          BufferedReader中的字符逐行讀取,如下所示。所有HTML內(nèi)容都被放入字符串變量中。使用safeCloseBufferedReader方法,當(dāng)我們完成時(shí),我們關(guān)閉緩沖區(qū)。

          我們可以將我們的HTML內(nèi)容發(fā)送到setVariableValue方法中,以便用我們從外部發(fā)送的值替換我們在服務(wù)中發(fā)送的值。我們在HTML中標(biāo)記為#key#的鍵值將被值值替換。

              private String setVariableValue(String htmlContent, String key, String value) {
                  if (StringUtils.isNotEmpty(value)) {
                      htmlContent = htmlContent.replaceAll("#"+key+"#", value);
                  }else {
                      htmlContent = htmlContent.replaceAll("#"+key+"#", "");
                  }
                  return htmlContent;
              }

          然后,在替換過程之后,我們可以調(diào)用ConvertHtmlToPdfUtil.htmlConvertToPdf方法,將HTML URL文件生成為PDF輸出。ConvertHtmlToPdfUtil.htmlConvertToPdf方法可以接收HTML內(nèi)容、輸出和字體輸入,如下所示。

          我們可以將這些輸入傳遞給該方法。

                        ConvertHtmlToPdfUtil.htmlConvertToPdf(htmlContent, outputFile, fontFile);

          ConvertHtmlToPdfUtil.htmlConvertToPdf方法內(nèi)容;

          我們將創(chuàng)建一個(gè)新的FileOutputStream。這將確定我們指定的output.pdf文件的創(chuàng)建。

          PdfRendererBuilder類位于com.openhtmltopdf.pdfboxout庫中。因此,我們必須將此庫添加到pom.xml文件中,如下所示。

                <dependency>
                      <groupId>com.openhtmltopdf</groupId>
                      <artifactId>openhtmltopdf-pdfbox</artifactId>
                      <version>1.0.10</version>
                  </dependency>
          <?xml version="1.0" encoding="UTF-8"?>
          <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
              xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
              <modelVersion>4.0.0</modelVersion>
              <parent>
                  <groupId>org.springframework.boot</groupId>
                  <artifactId>spring-boot-starter-parent</artifactId>
                  <version>3.1.1</version>
                  <relativePath/> <!-- lookup parent from repository -->
              </parent>
              <groupId>com.works</groupId>
              <artifactId>convertHtmlToPDF</artifactId>
              <version>0.0.1-SNAPSHOT</version>
              <name>convertHtmlToPDF</name>
              <description>convertHtmlToPDF</description>
              <properties>
                  <java.version>17</java.version>
                  <spring-cloud.version>2022.0.3</spring-cloud.version>
              </properties>
              <dependencies>
                  <dependency>
                      <groupId>org.springframework.boot</groupId>
                      <artifactId>spring-boot-starter</artifactId>
                  </dependency>
                  <dependency>
                      <groupId>org.springframework.boot</groupId>
                      <artifactId>spring-boot-starter-web</artifactId>
                  </dependency>
                  <dependency>
                      <groupId>com.openhtmltopdf</groupId>
                      <artifactId>openhtmltopdf-pdfbox</artifactId>
                      <version>1.0.10</version>
                  </dependency>
                  <dependency>
                      <groupId>org.springframework.cloud</groupId>
                      <artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
                      <version>4.0.1</version>
                  </dependency>
                  <dependency>
                      <groupId>org.springframework.boot</groupId>
                      <artifactId>spring-boot-starter-test</artifactId>
                      <scope>test</scope>
                  </dependency>
              </dependencies>
          
              <build>
                  <plugins>
                      <plugin>
                          <groupId>org.springframework.boot</groupId>
                          <artifactId>spring-boot-maven-plugin</artifactId>
                          <configuration>
                              <image>
                                  <builder>paketobuildpacks/builder-jammy-base:latest</builder>
                              </image>
                          </configuration>
                      </plugin>
                  </plugins>
              </build>
          
          </project>

          實(shí)現(xiàn)PdfRendererBuilder對(duì)象后,我們可以將HTML參數(shù)設(shè)置為'withHtmlContent',將fontpath參數(shù)設(shè)置為'useFont'。我們可以使用toStream設(shè)置輸出文件。最后,我們可以使用run方法運(yùn)行它。

                      pdfBuilder.useFastMode();
                      pdfBuilder.withHtmlContent(html, null);
                      String fontPath = fonts;
                      pdfBuilder.useFont(new File(concatPath(fontPath, "times.ttf")), "Times", null, null, false);
                      pdfBuilder.toStream(os);
                      pdfBuilder.run();

          pdfBuilder.run(); 在運(yùn)行該方法后,我們應(yīng)該看到output.pdf文件被創(chuàng)建在output文件夾下。

          因此,我們可以看到使用openhtmltopdf-pdfbox庫進(jìn)行平滑的HTML到PDF轉(zhuǎn)換過程。


          主站蜘蛛池模板: 水蜜桃av无码一区二区| 无码人妻久久久一区二区三区| 日本一区二区三区在线视频观看免费| 白丝爆浆18禁一区二区三区| 无码一区二区三区视频| 欧洲精品免费一区二区三区| 亚洲狠狠狠一区二区三区| 国产一区美女视频| 国产精品综合一区二区三区| 亚洲乱色熟女一区二区三区蜜臀| 久久久久人妻一区精品果冻| 国产一区二区视频免费| 天天躁日日躁狠狠躁一区| 亚洲AV无码一区二区三区网址 | 亚洲国产一区在线| 国产一区三区二区中文在线| 果冻传媒一区二区天美传媒| 无码国产亚洲日韩国精品视频一区二区三区| 中文字幕在线一区| 国产在线视频一区| 中文字幕精品无码一区二区三区 | 麻豆AV一区二区三区久久| 国产一区二区三区91| 亚洲日韩激情无码一区| 伊人色综合一区二区三区| 国产美女精品一区二区三区| 国产精品伦一区二区三级视频 | 亚洲AV综合色一区二区三区| 亚洲AV本道一区二区三区四区| 国产熟女一区二区三区四区五区 | 亚洲av无码一区二区三区观看 | 日本一区二区三区不卡视频| 精品国产一区在线观看| 暖暖免费高清日本一区二区三区 | 麻豆AV一区二区三区久久| 亚洲香蕉久久一区二区三区四区| 精产国品一区二区三产区| 日韩精品中文字幕视频一区| 久久精品国产第一区二区| 久久人妻av一区二区软件| 无码人妻一区二区三区兔费|