Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge multiple fodt files and convert to PDF #42

Closed
ikr0m opened this issue Jan 18, 2018 · 9 comments
Closed

Merge multiple fodt files and convert to PDF #42

ikr0m opened this issue Jan 18, 2018 · 9 comments

Comments

@ikr0m
Copy link

ikr0m commented Jan 18, 2018

I'm using old version of jodconverter 2.2.1. I don't know if it's possible to merge multiple fodt files and then convert to pdf file. Now I can convert 1 fodt file to pdf.
I find this fork, I want to use it if it supports converting multiple files to 1 pdf file (merge then convert).
For example: I've a.fodt, b.fodt, c.fodt, extra.fodt files. I want this a.fodt + extra.fodt => result.pdf, b.fodt + extra.fodt => result2.pdf, ...

@ikr0m
Copy link
Author

ikr0m commented Jan 18, 2018

I'm using java 8, playframework, scala, sbt.

@sbraconnier
Copy link
Member

sbraconnier commented Jan 18, 2018

Out of the box it is not possible, but the missing parts could be added with little effort. I find the idea quite interesting so I will check how to do this (within a week) and come back to you with my results.

The missing parts are that fodt is not supported by default (but can be added easily, I'll open an issue to support it by default) and the other part would be to create a Filter that would merge a document to the original document being converted. Such a filter could then be added to the filter chain for each document to merge.

This was referenced Jan 20, 2018
@sbraconnier
Copy link
Member

Could you please try the latest version of the master branch? You will have to use the new DocumentInserterFilter in order to merge documents. Using the same example you provided:

final DocumentInserterFilter inserterFilter = new DocumentInserterFilter(new File("extra.fodt"));

// Merge a.fodt with extra.fodt to produce result.pdf
LocalConverter
  .builder()
  .filterChain(inserterFilter)
  .build()
  .convert(new File("a.fodt"))
  .to(new File("result.pdf"))
  .execute();

// Merge b.fodt with extra.fodt to produce result2.pdf
LocalConverter
  .builder()
  .filterChain(inserterFilter)
  .build()
  .convert(new File("b.fodt"))
  .to(new File("result2.pdf"))
  .execute();

Note that you can merge multiple documents. The following example would merge a.fodt, b.fodt, c.fodt and extra.fodt to produce result.pdf:

final DocumentInserterFilter bFilter = new DocumentInserterFilter(new File("b.fodt"));
final DocumentInserterFilter cFilter = new DocumentInserterFilter(new File("c.fodt"));
final DocumentInserterFilter extraFilter = new DocumentInserterFilter(new File("extra.fodt"));

// Merge a.fodt with extra.fodt to produce result.pdf
LocalConverter
  .builder()
  .filterChain(bFilter , cFilter, extraFilter)
  .build()
  .convert(new File("a.fodt"))
  .to(new File("result.pdf"))
  .execute();

@ikr0m
Copy link
Author

ikr0m commented Jan 21, 2018

Thank you for positive response and effort! I'll try to use it soon.

@ikr0m
Copy link
Author

ikr0m commented Feb 1, 2018

Tried to concatenate 2 fodt files and generate pdf file:

val fodt1 = Paths.get("/path/to/file1.fodt").toFile
val fodt2 = Paths.get("/path/to/file2.fodt").toFile
val pdfMerged = Paths.get("/path/to/merged_3.pdf").toFile

val inserterFilter = new DocumentInserterFilter(fodt1)

LocalConverter
    .builder()
    .filterChain(inserterFilter)
    .build()
    .convert(fodt2)
    .to(pdfMerged)
    .execute()

But merged_3.pdf file contains only file1.fodt file content. It's ignoring fodt2.

@sbraconnier
Copy link
Member

Which version of LibreOffice is it ? Is it possible for you to provide me with files (removing all sensitive data) I could use to reproduce your problem. On my end, everything works just fine.

Can you try the 4.1.1-SNAPSHOT version available in the OSS snapshots repository:
https://oss.sonatype.org/content/repositories/snapshots/

@ikr0m
Copy link
Author

ikr0m commented Feb 2, 2018

libreoffice version is LibreOffice 5.1.6.2 10m0(Build:2)
This is the code:

  val officeManager = LocalOfficeManager.builder().install().build()
  officeManager.start()
  val fodt1 = Paths.get("/home/user/tmp/files/test1.fodt").toFile
  val fodt2 = Paths.get("/home/user/tmp/files/test2.fodt").toFile
  val pdfMerged = Paths.get("/home/user/tmp/files/test_result.pdf").toFile
  val inserterFilter = new DocumentInserterFilter(fodt1)
  LocalConverter
      .builder()
      .filterChain(inserterFilter)
      .build()
      .convert(fodt2)
      .to(pdfMerged)
      .execute()

Attaching 3 files. Input (test1.fodt, test2.fodt) and output (test_result.pdf). Archived fodt files because github didn't allow to attach them.
test_result.pdf
test_fodt_files.tar.gz

@sbraconnier
Copy link
Member

That one is not easy... It seems to be related to "fodt" format. If you convert the fodt to odt first, it works:

  val officeManager = LocalOfficeManager.builder().install().build()
  officeManager.start()
  val fodt1 = Paths.get("/home/user/tmp/files/test1.fodt").toFile
  val odt1 = Paths.get("/home/user/tmp/files/test1.odt").toFile
  val fodt2 = Paths.get("/home/user/tmp/files/test2.fodt").toFile
  val odt2 = Paths.get("/home/user/tmp/files/test2.odt").toFile
  val pdfMerged = Paths.get("/home/user/tmp/files/test_result.pdf").toFile

  LocalConverter.make().convert(fodt1).to(odt1).execute()
  LocalConverter.make().convert(fodt2).to(odt2).execute()

  val inserterFilter = new DocumentInserterFilter(odt1)

  LocalConverter
      .builder()
      .filterChain(inserterFilter)
      .build()
      .convert(odt2)
      .to(pdfMerged)
      .execute()

I'll try to find out why, but I'm not that confident that I'll find a better option...

@ikr0m
Copy link
Author

ikr0m commented Feb 4, 2018

Thanks! That is useful too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants