Code Monkey home page Code Monkey logo

Comments (10)

sbraconnier avatar sbraconnier commented on May 24, 2024

sfd stands for "store FilterData", which is applied when the document is stored (saved). This is why sfdPageRange works. But the other 2 arguments, I think they should be applied when the document is loaded (opened), so try with lfd instead, which stands for "load FilterData":

import okhttp3.*;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

public class Main2 {
    public static void main(String[] args) throws IOException {
        OkHttpClient client = new OkHttpClient();
        String url = "http://192.168.0.175:8080/lool/convert-to";

        File file = new File("C:\\Users\\highelf\\Downloads\\test2.pptx");
        RequestBody requestBody = new MultipartBody.Builder()
                .setType(MultipartBody.FORM)
                .addFormDataPart("data", file.getName(),
                        RequestBody.create(file, MediaType.parse("application/octet-stream")))
                .addFormDataPart("format", "pdf")
                .addFormDataPart("sfdPageRange", "3-3")
                .addFormDataPart("lfdEncryptFile", "true")
                .addFormDataPart("lfdDocumentOpenPassword", "1234")
                .build();

        Request request = new Request.Builder()
                .url(url)
                .post(requestBody)
                .build();

        try (Response response = client.newCall(request).execute()) {
            if (response.isSuccessful()) {
                File outputFile = new File("C:\\Users\\highelf\\Documents\\output9.pdf");
                try (FileOutputStream fos = new FileOutputStream(outputFile)) {
                    fos.write(response.body().bytes());
                }
                System.out.println("File uploaded and converted successfully.");
            } else {
                System.out.println("Error occurred: " + response.code());
            }
        }
    }
}

I didn't test it but it's worth a try...

from jodconverter.

wiedor avatar wiedor commented on May 24, 2024

REST log is

2024-02-06T00:30:12.501Z DEBUG 1 --- [JODConverter Sample Rest Api] [ter-poolentry-2] o.j.l.o.LocalOfficeManagerPoolEntry      : Limit of 200 maximum tasks per process not reached yet. Task count is 17
2024-02-06T00:30:12.501Z DEBUG 1 --- [JODConverter Sample Rest Api] [nio-8080-exec-9] o.j.c.o.AbstractOfficeManagerPoolEntry   : Task executed successfully: LocalConversionTask{source=SourceDocumentSpecsFromInputStream{file=null, format=null}, loadProperties={ReadOnly=true, Hidden=true, EncryptFile=true, UpdateDocMode=0, DocumentOpenPassword=1234}, target=TargetDocumentSpecsFromOutputStream{file=null, format=pdf}, storeProperties={FilterData={PageRange=3-3}}, useStreamAdapters=false}
2024-02-06T00:30:12.501Z DEBUG 1 --- [JODConverter Sample Rest Api] [nio-8080-exec-9] o.j.c.office.AbstractOfficeManagerPool   : Returning office manager to the pool...

As you told me, I tested it with lfd, but
The created document is opened without requiring a password.

What's a bit questionable is that if you look at the log,
sfd goes into filter data
lfd appears to just go in

I have one more question

Limit of 200 maximum tasks per process not reached yet. Task count is 17

Does this mean that if it goes over 200, it can no longer be used?
Or do you mean start the process again?

from jodconverter.

sbraconnier avatar sbraconnier commented on May 24, 2024

I've tested the lfd parameters locally and it worked as expected on my end:

2024-02-06T11:01:58.844-05:00 DEBUG 9680 --- [JODConverter Sample Rest Api] [ter-poolentry-2] o.j.l.o.LocalOfficeManagerPoolEntry      : Executing task: LocalConversionTask{source=SourceDocumentSpecsFromInputStream{file=null, format=null}, loadProperties={ReadOnly=true, Hidden=true, FilterData={FilterData={EncryptFile=true, DocumentOpenPassword=12634}}, UpdateDocMode=0}, target=TargetDocumentSpecsFromOutputStream{file=null, format=pdf}, storeProperties={FilterData={FilterData={PageRange=3-3}}, FilterName=writer_pdf_Export}, useStreamAdapters=false}
2024-02-06T11:01:58.846-05:00  INFO 9680 --- [JODConverter Sample Rest Api] [ter-poolentry-2] o.j.local.task.LocalConversionTask       : Executing local conversion task [? -> pdf]...
2024-02-06T11:02:00.264-05:00 DEBUG 9680 --- [JODConverter Sample Rest Api] [ter-poolentry-2] o.j.l.o.LocalOfficeManagerPoolEntry      : Task executed successfully: LocalConversionTask{source=SourceDocumentSpecsFromInputStream{file=null, format=null}, loadProperties={ReadOnly=true, Hidden=true, FilterData={FilterData={EncryptFile=true, DocumentOpenPassword=12634}}, UpdateDocMode=0}, target=TargetDocumentSpecsFromOutputStream{file=null, format=pdf}, storeProperties={FilterData={FilterData={PageRange=3-3}}, FilterName=writer_pdf_Export}, useStreamAdapters=false}
2024-02-06T11:02:00.265-05:00 DEBUG 9680 --- [JODConverter Sample Rest Api] [ter-poolentry-2] o.j.l.o.LocalOfficeManagerPoolEntry      : Limit of 200 maximum tasks per process not reached yet. Task count is 1
2024-02-06T11:02:00.265-05:00 DEBUG 9680 --- [JODConverter Sample Rest Api] [nio-8080-exec-1] o.j.c.o.AbstractOfficeManagerPoolEntry   : Task executed successfully: LocalConversionTask{source=SourceDocumentSpecsFromInputStream{file=null, format=null}, loadProperties={ReadOnly=true, Hidden=true, FilterData={FilterData={EncryptFile=true, DocumentOpenPassword=12634}}, UpdateDocMode=0}, target=TargetDocumentSpecsFromOutputStream{file=null, format=pdf}, storeProperties={FilterData={FilterData={PageRange=3-3}}, FilterName=writer_pdf_Export}, useStreamAdapters=false}

Which jodconverter version are you using?

Limit of 200 maximum tasks per process not reached yet. Task count is 17.
Does this mean that if it goes over 200, it can no longer be used?
Or do you mean start the process again?

Jodconverter uses a pool of LibreOffice/OpenOffice processes, in the same way HikariCP uses a pool of database connections.
the number of processes is controlled by the portNumbers property. In some version of LibreOffice/OpenOffice, the RAM taken by a process keeps growing a little for each document conversion, as if there were some sort of memory leak. The maxTaskPerProcess property specifies the maximum number of conversions a single process can do before it is stopped (or killed if it cannot be stopped gracefully) and then restarted. You don't have to worry about restarting the process youself, jodconverter will do it.

from jodconverter.

wiedor avatar wiedor commented on May 24, 2024

Thank you so much for your time and effort in confirming and responding.

I found the Docker image on the website below.
https://github.com/jodconverter/docker-image-jodconverter-examples

I ran the container using the Docker command below:

docker run -d --name jodrest -p 8080:8080 ghcr.io/jodconverter/jodconverter-examples:rest

When I entered the container, extracted the war, and unzipped it, I saw an item called jodconverter-local-4.4.7.jar.

image

this is startup log in container
log1.txt

from jodconverter.

sbraconnier avatar sbraconnier commented on May 24, 2024

Thank you so much for your time and effort in confirming and responding.

My pleasure!

Ok... so well I followed the same steps and here's my output:

2024-02-07T05:11:01.536Z DEBUG 1 --- [JODConverter Sample Rest Api] [ter-poolentry-3] o.j.l.o.LocalOfficeManagerPoolEntry      : Limit of 200 maximum tasks per process not reached yet. Task count is 1
2024-02-07T05:11:01.536Z DEBUG 1 --- [JODConverter Sample Rest Api] [nio-8080-exec-4] o.j.c.o.AbstractOfficeManagerPoolEntry   : Task executed successfully: LocalConversionTask{source=SourceDocumentSpecsFromInputStream{file=null, format=null}, loadProperties={ReadOnly=true, Hidden=true, FilterData={EncryptFile=true, DocumentOpenPassword=1234}, UpdateDocMode=0}, target=TargetDocumentSpecsFromOutputStream{file=null, format=pdf}, storeProperties={FilterData={PageRange=3-3}}, useStreamAdapters=false}
2024-02-07T05:11:01.536Z DEBUG 1 --- [JODConverter Sample Rest Api] [nio-8080-exec-4] o.j.c.office.AbstractOfficeManagerPool   : Returning office manager to the pool...

So everything looks OK to me.... maybe you don't have the latest changes from the repository? Have your tried git pull on your docker-image-jodconverter-examples repository?

Can you paste here your client code again with the new lfd parameters? Here's mine (both version works)

OkHttpClient client = new OkHttpClient();
String url = "http://localhost:8080/lool/convert-to";

File file = new File("C:\\tmp\\test.doc");
RequestBody requestBody = new MultipartBody.Builder()
        .setType(MultipartBody.FORM)
        .addFormDataPart("data", file.getName(),
                RequestBody.create(file, MediaType.parse("application/octet-stream")))
        .addFormDataPart("format", "pdf")
        .addFormDataPart("sfdPageRange", "3-3")
        .addFormDataPart("lfdEncryptFile", "true")
        .addFormDataPart("lfdDocumentOpenPassword", "1234")
        .build();

Request request = new Request.Builder()
        .url(url)
        .post(requestBody)
        .build();

try (Response response = client.newCall(request).execute()) {
    if (response.isSuccessful()) {
        File outputFile = new File("C:\\tmp\\test.pdf");
        try (FileOutputStream fos = new FileOutputStream(outputFile)) {
            fos.write(response.body().bytes());
        }
        System.out.println("File uploaded and converted successfully.");
    } else {
        System.out.println("Error occurred: " + response.code());
    }
}
OkHttpClient client = new OkHttpClient();
String url = "http://localhost:8080/lool/convert-to";

File file = new File("C:\\tmp\\test.doc");
RequestBody requestBody = new MultipartBody.Builder()
        .setType(MultipartBody.FORM)
        .addFormDataPart("data", file.getName(),
                RequestBody.create(file, MediaType.parse("application/octet-stream")))
        .build();

Request request = new Request.Builder()
        .url(HttpUrl.parse(url).newBuilder()
                .addQueryParameter("format", "pdf")
                .addQueryParameter("sfdPageRange", "3-3")
                .addQueryParameter("lfdEncryptFile", "true")
                .addQueryParameter("lfdDocumentOpenPassword", "1234")
                .build())
        .post(requestBody)
        .build();

try (Response response = client.newCall(request).execute()) {
    if (response.isSuccessful()) {
        File outputFile = new File("C:\\tmp\\test.pdf");
        try (FileOutputStream fos = new FileOutputStream(outputFile)) {
            fos.write(response.body().bytes());
        }
        System.out.println("File uploaded and converted successfully.");
    } else {
        System.out.println("Error occurred: " + response.code());
    }
}

from jodconverter.

wiedor avatar wiedor commented on May 24, 2024

omg I'm so sorry

The reason there was no FilterData in loadProperty in the log I provided was
I think I tested the login with "l" instead of "lfd" because I couldn't do it.

Sorry for the confusion.

The log came out identical to the source you provided.

2024-02-07T06:59:02.218Z DEBUG 1 --- [JODConverter Sample Rest Api] [ter-poolentry-3] o.j.l.o.LocalOfficeManagerPoolEntry      : Limit of 200 maximum tasks per process not reached yet. Task count is 2
2024-02-07T06:59:02.220Z DEBUG 1 --- [JODConverter Sample Rest Api] [nio-8080-exec-7] o.j.c.o.AbstractOfficeManagerPoolEntry   : Task executed successfully: LocalConversionTask{source=SourceDocumentSpecsFromInputStream{file=null, format=null}, loadProperties={ReadOnly=true, Hidden=true, FilterData={EncryptFile=true, DocumentOpenPassword=1234}, UpdateDocMode=0}, target=TargetDocumentSpecsFromOutputStream{file=null, format=pdf}, storeProperties={FilterData={PageRange=3-3}}, useStreamAdapters=false}
2024-02-07T06:59:02.220Z DEBUG 1 --- [JODConverter Sample Rest Api] [nio-8080-exec-7] o.j.c.office.AbstractOfficeManagerPool   : Returning office manager to the pool...

However, in my case, when I open the created PDF file in Windows,
Chrome launches and doesn't ask for a password.

Does the file you created ask for a password when you run it?

from jodconverter.

sbraconnier avatar sbraconnier commented on May 24, 2024

It's my turn to apologize, I misread your original problem. I thought you were trying to convert an already encrypted document, not produce one. So, you should stick with sfd prefix for all three arguments.

It turns out that your original code is the way to go. After a little debugging, I found out that your problem is the actual password you chose for your example, because of this line of code that will interpret your argument as an Integer instead of a String, which seems to be ignored by LibreOffice since it expects a String. I'll try to fix this, but in the meantime, you can force a String by introducing an alpha character to your password. Here's an example that works for me (I was ask for a password when opening the converted PDF document):

OkHttpClient client = new OkHttpClient();
String url = "http://localhost:8080/lool/convert-to";

File file = new File("C:\\tmp\\test.doc");
RequestBody requestBody = new MultipartBody.Builder()
        .setType(MultipartBody.FORM)
        .addFormDataPart("data", file.getName(),
                RequestBody.create(file, MediaType.parse("application/octet-stream")))
        .addFormDataPart("format", "pdf")
        .addFormDataPart("sfdPageRange", "3-3")
        .addFormDataPart("sfdEncryptFile", "true")
        .addFormDataPart("sfdDocumentOpenPassword", "abc1234")
        .build();

Request request = new Request.Builder()
        .url(url)
        .post(requestBody)
        .build();

try (Response response = client.newCall(request).execute()) {
    if (response.isSuccessful()) {
        File outputFile = new File("C:\\tmp\\test.pdf");
        try (FileOutputStream fos = new FileOutputStream(outputFile)) {
            fos.write(response.body().bytes());
        }
        System.out.println("File uploaded and converted successfully.");
    } else {
        System.out.println("Error occurred: " + response.code());
    }
}

from jodconverter.

sbraconnier avatar sbraconnier commented on May 24, 2024
  1. Is there an option to change the size of all converted PDF pages to A4?

No there isn't a way to do that remotely using the rest API example. Locally, it would look like this:

Filter a4FormatFilter = (context, document, chain) -> {

    Lo.qi(XPrintable.class, document).setPrinter(
            Props.makeProperties(
                    "PaperFormat", PaperFormat.A4,
                    "PaperSize", new Size(21000, 29700) // 21,00 cm x 29,70 cm
            )
    );

    chain.doFilter(context, document);
};

final File inputFile = new File("C:\\tmp\\test.doc");
final File outputFile = new File("C:\\tmp\\test.pdf");

final Map<String, Object> filterData = new HashMap<>();
filterData.put("PageRange", "3-3");
filterData.put("EncryptFile", true);
filterData.put("DocumentOpenPassword", "a1234");

LocalConverter
        .builder()
        .officeManager(manager)
        .filterChain(a4FormatFilter)
        .storeProperty("FilterData", filterData)
        .build()
        .convert(inputFile)
        .to(outputFile)
        .execute();

But you probably could have your own [https://github.com/jodconverter/jodconverter-samples/tree/main/samples/spring-boot-rest](REST Api) and interpret whatever parameters you would like/need. Mine was just a short sample to help people start with their own project.

from jodconverter.

wiedor avatar wiedor commented on May 24, 2024

I used "sfd" as you suggested and entered "abc1234" as the password.
Finally, a password notification appears.

Who would have thought that a password couldn't contain only numbers?

thank you very much

The spring rest version you created is
It's already great in itself, and it seems to have endless uses since it can communicate with the container and send and receive files via http.

After receiving the PDF file with jod rest
I plan to try merging, splitting, etc. with other PDF libraries (iText, pdfbox, etc.)

Anyway, converting MS documents into PDF was the most difficult part.

Thank you once again for revealing the great source.
I wish you all the best in your path ahead.

from jodconverter.

sbraconnier avatar sbraconnier commented on May 24, 2024

Glad I could help!!! Good luck with your project!!

from jodconverter.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.