Goutham Goutham - 1 month ago 19
Scala Question

HTML to PDF Generation in Scala Language

I have already done Pdf generation from HTML in JAVA and it worked fine.
But I need this to be done with "scala". Even though I import all the jars as did in JAVA there comes a problem with IText Renderer.
Please suggest me the same to achieve the output.

Java Code:

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 Code :

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();

}

}


Thank you in advance..!

Answer

Define render as 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()

  }

}
Comments