Skip to main content
Announcements
A fresh, new look for the Data Integration & Quality forums and navigation! Read more about what's changed.
cancel
Showing results for 
Search instead for 
Did you mean: 
MRoldan1633480871
Contributor

Fetch PDF document and convert it to Base64

Hi all. Im kind of newbie in all these things.

I need to get a document from a URL with a parameter (e.g. http://localhost?id_document=1,

http://localhost?id_document=2

). I used the tFetchFile componente, but I did not find how to pass the query param to identify the document. Can someone help me?

Then, I need to convert that file to base64. Does anybody know what component I should use?

Thanks in advance.

Labels (2)
1 Solution

Accepted Solutions
gjeremy1617088143

HI @Maxi Roldan​ , you can format directly you URI in tfileFetch : " http://localhost?id_document=" + global var of your id.

The you can use a tFileList on the local folder you store the document,

then you can use this routine for example to transform the content into base64 with the file path from tFileList :

 

package routines;

import java.nio.file.Files;

import java.nio.file.Paths;

/*

 

*/

public class File_managing {

public static String base64FromFile(String filepath) {

try{

  byte[] incoming_file_data = Files.readAllBytes(Paths.get(filepath));

  return Base64.encode(incoming_file_data);

}

catch(Exception err){

  err.printStackTrace();return null;

  }

 }

}

 

 

Send me Love and Kudos

View solution in original post

5 Replies
gjeremy1617088143

HI @Maxi Roldan​ , you can format directly you URI in tfileFetch : " http://localhost?id_document=" + global var of your id.

The you can use a tFileList on the local folder you store the document,

then you can use this routine for example to transform the content into base64 with the file path from tFileList :

 

package routines;

import java.nio.file.Files;

import java.nio.file.Paths;

/*

 

*/

public class File_managing {

public static String base64FromFile(String filepath) {

try{

  byte[] incoming_file_data = Files.readAllBytes(Paths.get(filepath));

  return Base64.encode(incoming_file_data);

}

catch(Exception err){

  err.printStackTrace();return null;

  }

 }

}

 

 

Send me Love and Kudos

MRoldan1633480871
Contributor
Author

Hi Jeremy. Many thanks for your answer! It was really helpful. I need to convert the file without saving it on disk. Do you know how I can di this? Thanks in advance.

gjeremy1617088143

HI, you can use Use cache to save the resource on the tfilefetch to store the file on an input stream.

Send me Love and Kudos

MRoldan1633480871
Contributor
Author

Hi Jeremy. Thanks again. How can I read that input stream? With a tFileInputDelimited? Thanks in advance.

gjeremy1617088143

you can try something like this to get the byte[] from input stream:

 

 

ByteArrayOutputStream out = new ByteArrayOutputStream();

byte[] buffer = new byte[1024];

while (true) {

int r = ((java.io.InputStream)globalMap.get("tFileFetch_1_INPUT_STREAM"))).read(buffer);

if (r == -1) break;

out.write(buffer, 0, r);

}

 

byte[] ret = out.toByteArray();

 

 

then you convert to base 64

Send me love and kudos