1

I have an SVG graphic generated by the JS libraray D3 which should be saved on the clients computer. I want to offer different Formats and while i found pure JS solution for downloading SVG and PNG, i´m kind of stuck at also offering PDF. As far as i can see there is no pure Client solution for this which supports ALL SVG elements (i tried jsPDF), although i wondered why there is no easier solution. Anyway, that is why i want to use a servlet which uses the java batik library (the same way as in this topic: Convert SVG to PDF)

Now i wanted to take my SVG element, convert it into a string and then make an ajax get request:

    var svg = $("#chart").get(0);
    var serializer = new XMLSerializer();
    var sXML = serializer.serializeToString(svg);

$.get("http://localhost:8080/Webmap/Conversion", 
            {svg: svg},
            function (){
                //how do i handle the response object?
        });

In my servlet, i want to take the string, convert the content and retrieve it to the client so that it can be downloaded:

protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    String svgString = req.getParameter("svg");
    StringReader svg = new StringReader(svgString);
    File pdf = new File("/tmp/chart.pdf");

    Transcoder transcoder = new PDFTranscoder();
    TranscoderInput transcoderInput = new TranscoderInput(svg);
    TranscoderOutput transcoderOutput = new TranscoderOutput(new  FileOutputStream(pdf));
    transcoder.transcode(transcoderInput, transcoderOutput);

    resp.setContentType("application/pdf");
    resp.setHeader("Content-Disposition", "attachment; filename=/tmp /chart.pdf");

    //...how do i have to wrap the generated pdf file into the resp object?
}

Sorry, if that seems like a simple question, but what i basically do not understand now is how do i attach the pdf to the response and how can i offer it as a download in the browser?

Community
  • 1
  • 1
flixe
  • 626
  • 1
  • 11
  • 36
  • you can find many explanations and examples on this subject, just google around: http://stackoverflow.com/questions/10982153/servlet-pdf-download-button-creation cheers – jsfviky Dec 08 '15 at 21:41

0 Answers0