package controllers import play.api._ import play.api.mvc._ import java.io._ import org.apache.batik.dom.svg._ import org.apache.batik.transcoder._ import org.apache.batik.transcoder.image._ import org.apache.batik.util.XMLResourceDescriptor; import org.w3c.dom.Document import java.awt.Rectangle import org.apache.fop.svg.PDFTranscoder import play.api.libs.iteratee._ import play.api.data._ import play.api.data.Forms._ case class Image( filename:String, scale:Int, imagetype:String, svg:String ) object Application extends Controller { val imageDataForm = Form( mapping( "filename"->text, "scale"->number, "type"->text, "svg"->text )(Image.apply)(Image.unapply) ) def index()=Action{implicit request => Ok(views.html.index()) } def makeImage()=Action{implicit request => val image=imageDataForm.bindFromRequest.get val is=new ByteArrayInputStream(image.svg.getBytes()) import scala.concurrent.ExecutionContext.Implicits._ Ok.chunked(Enumerator.outputStream { os => val tin=new TranscoderInput(is) val tot=new TranscoderOutput(os) getTranscoder(image.imagetype, 0.7f) match{ case Some(x) =>{ println("transcode:x="+x) x.transcode(tin,tot) } case _ => None } os.flush os.close println("flush") } >>> Enumerator.eof).withHeaders( "Content-Type" -> (image.imagetype), "Content-Disposition"->("attachment; filename="+image.filename) ) } def getParameter(request:play.api.mvc.Request[play.api.mvc.AnyContent],str:String) = { request.queryString.get(str).flatMap(_.headOption) match { case Some(x) => x case None => "" } } def getDocument(is:InputStream):Document ={ val parser = XMLResourceDescriptor.getXMLParserClassName() val f = new SAXSVGDocumentFactory(parser) val doc = f.createDocument("http://www.w3.org/2000/svg",is) return doc } def getTranscoder( transcoderType:String, keyQuality:Float ) :Option[Transcoder]={ println("getTranscoder:"+transcoderType) transcoderType match{ case "image/jpeg" =>Option(getJPEGTranscoder(keyQuality)) case "image/png" => Option(getPNGTranscoder()) case "application/pdf" => Option(new PDFTranscoder()) case _ => None } } def getJPEGTranscoder( keyQuality:Float):JPEGTranscoder= { val jpeg = new JPEGTranscoder() jpeg.addTranscodingHint(JPEGTranscoder.KEY_QUALITY, keyQuality) return jpeg } def getPNGTranscoder() :PNGTranscoder={ return new PNGTranscoder(); } }
index.scala.html
こんな感じでいけるかと思います