I want to automate the process of sending a PDF file, which is available in my Tape environment (via the link invoice_field_rechnungen_file_url from Filefield Rechnungen in Tape), to the Scopevisio service. The process involves the following steps:
- Downloading the PDF file from Tape:
I have a link (invoice_field_rechnungen_file_url) pointing to a PDF file. This file should be retrieved by my script to obtain the raw content of the PDF.
- Converting the downloaded PDF file to Base64:
Scopevisio expects the file to be sent as a Base64-encoded string, not as a URL or binary data. Therefore, I need to convert the downloaded PDF file into a Base64 string.
- Sending the Base64-encoded content to Scopevisio:
Using an API request (HTTP POST), I want to send the Base64-encoded string along with the filename to Scopevisio. The service should then create a new incoming invoice and attach the provided PDF as the receipt.
Problem:
The file is successfully uploaded to Scopevisio, but it cannot be opened because it is corrupted. However, files uploaded via Postman are readable and correctly displayed in Scopevisio.
In summary:
I want to ensure that the PDF file is not simply forwarded but properly transformed before being sent to Scopevisio, so that Scopevisio can recognize, store, and display the content as a readable PDF. The challenge lies in correctly downloading the PDF as binary data, encoding it to Base64, and formatting the JSON payload properly for the Scopevisio API.
{
// Zuerst das PDF von der URL laden (binär!)
const pdfResponse = await http.get(invoice_field_rechnungen_file_url, {
// falls möglich eine Option setzen, um Binärdaten zu erhalten
// responseType: 'arraybuffer', encoding: null, o.ä.
});
if (!pdfResponse || !pdfResponse.data) {
throw new Error('Konnte die PDF nicht herunterladen.');
}
// pdfResponse.data nun in einen Buffer umwandeln, falls noch keiner ist
let bufferData;
if (Buffer.isBuffer(pdfResponse.data)) {
bufferData = pdfResponse.data;
} else {
bufferData = Buffer.from(pdfResponse.data, 'binary');
}
// In Base64 umwandeln
const base64Data = bufferData.toString('base64');
// assemble headers
const request_headers = {
'Authorization': `Bearer 9cf8bbbccccccccb-e0c6e4-cccccccccccab85d5dd2bf`,
'Content-Type': 'application/json'
};
// Jetzt die base64-Daten statt der URL ĂĽbergeben
const request_body = JSON.stringify({
filename: invoice_field_rechnungen_filename || "",
data: base64Data
});
const request_options = {
followRedirects: false,
data: request_body,
headers: request_headers
};
const httpResult = await http.post('https://appload.scopevisio.com/rest/incominginvoice/new', request_options);
var_http_response = httpResult.data;
}
Her the cURL command for Scopevisio:
curl -X 'POST' \
'https://appload.scopevisio.com/rest/incominginvoice/new' \
-H 'accept: */*' \
-H 'Content-Type: application/json' \
-d '{
"filename": "string",
"data": "string"
}'
Required profiles: Rechnungseingangsbuch (Bearbeiten) .
The invoice should be provided as a base64 encoded pdf file. The maximum size allowed is 30 MB.
Does anyone have an idea how I can solve this?
Greetings Hans