HTML 到 Scala 语言的 PDF 生成

HTML to PDF Generation in Scala Language

我已经在 JAVA 中从 HTML 完成了 Pdf 生成,并且工作正常。 但我需要用 "scala" 来完成。尽管我像 JAVA 中那样导入了所有 jar,但 IText Renderer 出现了问题。 请建议我实现输出。

Java代码:

import java.io.*;
import com.lowagie.text.DocumentException;
import org.xhtmlrenderer.pdf.ITextRenderer;

public class Htmltopdf_hcue {

    public static void main(String[] args) throws IOException, DocumentException {
        String File_To_Convert = "F:/Pdf_Generation/Appointment Schedule_Fair_Edit.html";
        //String File_To_Convert = "C:/Users/hCue/Desktop/img.html";        
        String url = new File(File_To_Convert).toURI().toURL().toString();
        System.out.println(""+url);
        String HTML_TO_PDF = "F:/Pdf_Generation/TheEndPDF344444444444.pdf";
        OutputStream os = new FileOutputStream(HTML_TO_PDF);
        ITextRenderer renderer = new ITextRenderer();
        renderer.setDocument(url);      
        renderer.layout();
        renderer.createPDF(os);        
        os.close();
    }   
}

Scala 代码:

import java.awt.image.BufferedImage
import java.io.File

import java.io.FileOutputStream
import java.io.FileInputStream
import java.io.InputStreamReader
import java.net.URL
import javax.imageio.ImageIO
import java.io
import com.lowagie.text.DocumentException;
import org.xhtmlrenderer.pdf.ITextRenderer;
import org.xhtmlrenderer.pdf.ITextTextRenderer
object Hello{
    def main(args: Array[String]) { 
    var fos: FileOutputStream = null
    val File_To_Convert  ="F:/Pdf_Generation/Appointment Schedule_Fair_Edit.html";
    val url = new File(File_To_Convert).toURI().toURL().toString();
    val HTML_TO_PDF = "F:/Pdf_Generation/TheEndPDF344444444444.pdf";
    fos = new FileOutputStream(HTML_TO_PDF);
    ITextRenderer renderer = new ITextRenderer();/*Getting Error as Value renderer is not a member object of org.xhtmlrenderer.pdf.ITextRenderer */
        renderer.setDocument(url);      
        renderer.layout();
        renderer.createPDF(fos);  
        fos.close();

    } 

}

Build.sbt

//resolvers += "Typesafe repository" at "https://repo.typesafe.com/typesafe/releases/"
// The Typesafe repository
//resolvers += "Typesafe repository" at "http://repo.typesafe.com/typesafe/releases/"
resolvers += "Sonatype snapshots" at "https://oss.sonatype.org/content/repositories/snapshots"
// The Play plugin
//addSbtPlugin("com.typesafe.play" % "sbt-plugin" % "2.3.7")
// Use the Play sbt plugin for Play projects
addSbtPlugin("com.typesafe.play" % "sbt-plugin" % Option(System.getProperty("play.version")).getOrElse("2.3.1"))
// web plugins
addSbtPlugin("com.typesafe.sbt" % "sbt-coffeescript" % "1.0.0")
addSbtPlugin("com.typesafe.sbt" % "sbt-less" % "1.0.0")
addSbtPlugin("com.typesafe.sbt" % "sbt-jshint" % "1.0.1")
addSbtPlugin("com.typesafe.sbt" % "sbt-rjs" % "1.0.1")
addSbtPlugin("com.typesafe.sbt" % "sbt-digest" % "1.0.0")
addSbtPlugin("com.typesafe.sbt" % "sbt-mocha" % "1.0.0")
addSbtPlugin("com.github.play2war" % "play2-war-plugin" % "1.3-beta1")

提前谢谢你..!

定义渲染为val

import java.awt.image.BufferedImage
import java.io.File

import java.io.FileOutputStream
import java.io.FileInputStream
import java.io.InputStreamReader
import java.net.URL
import javax.imageio.ImageIO
import java.io
import com.lowagie.text.DocumentException
import org.xhtmlrenderer.pdf.ITextRenderer

object Hello{
  def main(args: Array[String]) {
    var fos: FileOutputStream = null
    val File_To_Convert  ="F:/Pdf_Generation/Appointment Schedule_Fair_Edit.html"
    val url = new File(File_To_Convert).toURI().toURL().toString()
    val HTML_TO_PDF = "F:/Pdf_Generation/TheEndPDF344444444444.pdf"
    fos = new FileOutputStream(HTML_TO_PDF)
    val renderer = new ITextRenderer()
    renderer.setDocument(url)
    renderer.layout()
    renderer.createPDF(fos)
    fos.close()

  }

}