1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 | 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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | <html> <head></head> <body> <form method="post" action="@routes.Application.makeImage()"> <input type="hidden" name="filename" value="chart" /> <input type="hidden" name="scale" value="2" /> <input type="hidden" name="type" value="application/pdf" /> <input type="hidden" name="svg" value=" <svg xmlns='http://www.w3.org/2000/svg' height='100' width='100'> <circle cx='50' cy='50' r='40' stroke='black' stroke-width='3' fill='red' /></svg> " /> <input type="submit" value="pdf"/> </form> <form method="post" action="@routes.Application.makeImage()"> <input type="hidden" name="filename" value="chart" /> <input type="hidden" name="scale" value="2" /> <input type="hidden" name="type" value="image/png" /> <input type="hidden" name="svg" value=" <svg xmlns='http://www.w3.org/2000/svg' height='100' width='100'> <circle cx='50' cy='50' r='40' stroke='black' stroke-width='3' fill='red' /></svg> " /> <input type="submit" value="png"/> </form> <form method="post" action="@routes.Application.makeImage()"> <input type="hidden" name="filename" value="chart" /> <input type="hidden" name="scale" value="2" /> <input type="hidden" name="type" value="image/jpeg" /> <input type="hidden" name="svg" value=" <svg xmlns='http://www.w3.org/2000/svg' height='100' width='100'> <circle cx='50' cy='50' r='40' stroke='black' stroke-width='3' fill='red' /></svg> " /> <input type="submit" value="jpeg"/> </form> </body> </html> |
こんな感じでいけるかと思います