Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Converting SVG to PDF

 
Goldi Ahuja
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello
I am using batik to create pdf documents out of my svg files which works perfect for one page images
However if i want to add detailed text just below the image and wish that pdf would display it in next page.
Anybody has any idea how to achieve this .
Basically my text is too long and I do not want my first page to have that text.
My code to convert svg to pdf goes here

TranscoderInput input = new TranscoderInput(svgURI);
OutputStream ostream = new FileOutputStream(outputFilename);
TranscoderOutput output = new TranscoderOutput(ostream);
trans.transcode(input, output);

Thanks in advance
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't think SVG 1.1 -which is what Batik implements- has a concept of "pages". That may be a feature of SVG 1.2, if that ever comes to pass.

The easiest solution might be to add a second page using the iText library after the SVG->PDF transformation is done.
 
Goldi Ahuja
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Ulf for a quick reply
but if my svg is converted to pdf using transcoder, the pdf would already have the entire output scaled to one page
I want to output one svg to span to two pages
DO you think it wud be gettable through itext?
Thanks again



 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As I said, I don't think Batik supports multi-page SVGs. And, as I also said, iText can be used to add a second page to an existing PDF.
 
Goldi Ahuja
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there any other alternative to batik to convert svg to multipage pdf?
thanks for your quick responses , its been a great help
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's unlikely, since no current SVG standard has a concept of multiple pages.

What's wrong with the approach I suggested?
 
Goldi Ahuja
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If i got it right ,
The approach that you suggest would first convert my svg to a one page pdf i.e my image would be scaled to one page , my charts would become very small
and then using itext i can add another page

but my initial image has already been squeezed by transcoder which is not what my employer desires
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As I understand it, the image only becomes small if there is a lot of text (or large text) underneath it; is that correct? If so, then that issue goes away if you move the text out of the SVG, and onto the second page of the PDF.
 
Goldi Ahuja
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
but the problem for me is the text is a part of my image only <text> and <tspan> tags which i cannot simple cut as a plain text
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can either change the process that creates the SVG, or -if that is not feasible- use DOM to remove the problematic tags from the SVG, and use the information embedded in those tags to create the second page of the PDF.
 
Goldi Ahuja
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not just text It has rectangles as well at the lower bottom of the page which needs to be printed in second page
Would it be possible to draw svg rectangles and texts in second page using itext ?
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
iText does not support SVG, but it can certainly draw lines, and consequently rectangles.

It may also be possible to split the SVG in half, thus creating two SVG files for the two pages. Then you wouldn't need to use iText at all.
 
Goldi Ahuja
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My data for svg is in memory and being formed dynamically , how to break it into two parts ? If I create two separate PDF files , still it wont solve the purpose as my client wants one pdf with 2 pages
how do i merge the two pdf in one ? is there any utility which could do this?

 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My data for svg is in memory and being formed dynamically , how to break it into two parts ?

If it is created in your Java code, maybe you can create two SVG files from the start? Alternatively - SVG is just XML, so any DOM library can be used to operate on it in any way you see fit.

how do i merge the two pdf in one ? is there any utility which could do this?

Libraries such as PDFBox and iText can do this.
 
Goldi Ahuja
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That means I form two svg files in my java DOM code and create two pdfs out of that and then merge them using itext
sounds good , I would try the same
Thanks a ton, I would keep you posted about the updates
Thanks again Ulf
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic