Skip to content

DOCINFRA-2341_merged_using_automation #393

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

Merged
merged 1 commit into from
Jul 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 91 additions & 2 deletions ej2-vue/pdfviewer/open-pdf-file/from-amazon-s3.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,95 @@ domainurl: ##DomainURL##

# Open PDF file from AWS S3

PDF Viewer allows to load PDF file from AWS S3 using either the Standalone or Server-backed PDF Viewer. Below are the steps and a sample to demonstrate how to open a PDF from AWS S3.

## Using Standalone PDF Viewer

To load a PDF file from AWS S3 in a PDF Viewer, you can follow the steps below.

**Step 1:** Create a PDF Viewer sample in Vue

Follow the instructions provided in this [link](https://ej2.syncfusion.com/vue/documentation/pdfviewer/getting-started) to create a simple PDF Viewer sample in Vue. This will set up the basic structure of your PDF Viewer application.

**Step 2:** Modify the `src/App.vue` File in the Vue Project

1. Import the required namespaces at the top of the file:

{% tabs %}
{% highlight html tabtitle="Options API (~/src/App.vue)" %}

<script>
import AWS from 'aws-sdk';
</script>

{% endhighlight %}
{% endtabs %}

2. Configures AWS SDK with the region, access key, and secret access key. This configuration allows the application to interact with AWS services like S3.

N> Replace **Your Region** with the actual Region of your AWS S3 account and **Your Access Key** with the actual Access Key of your AWS S3 account and **Your Security Access Key** with the actual Security Access Key of your AWS S3 account.

{% tabs %}
{% highlight html tabtitle="Options API (~/src/App.vue)" %}

<script>
AWS.config.update({
region: '**Your Region**', // Update this your region
accessKeyId: '*Your Access Key*', // Update this with your access key id
secretAccessKey: '*Your Security Access Key*', // Update this with your secret access key
});
</script>

{% endhighlight %}
{% endtabs %}

3. Sets the parameters for fetching the PDF document from S3, including the bucket name and file key. Then Uses the getObject method of the S3 instance to retrieve the document. Converts the document data to a Base64 string and loads it into the Syncfusion PDF Viewer then load Base64 string generated into the viewer.load method.

N> Replace **Your Bucket Name** with the actual Bucket name of your AWS S3 account and **Your Key** with the actual File Key of your AWS S3 account.


{% tabs %}
{% highlight html tabtitle="Options API (~/src/App.vue)" %}

<script>
export default {
methods: {
loadPdfDocument: function () {
const getObjectParams = {
Bucket: '**Your Bucket Name**',
Key: '**Your Key**',
};
var s3= new AWS.S3();
s3.getObject(getObjectParams, (err, data) => {
if (err) {
console.error('Error fetching document:', err);
} else {
if (data && data.Body) {
const bytes = new Uint8Array(data.Body);
let binary = '';
bytes.forEach((byte) => (binary += String.fromCharCode(byte)));
const base64String = window.btoa(binary);
setTimeout(() => {
var viewer = document.getElementById('pdfViewer').ej2_instances[0];
viewer.load("data:application/pdf;base64,"+base64String);
}, 2000);
}
}
});
},
}
}
</script>

{% endhighlight %}
{% endtabs %}

N> The **npm install aws-sdk** package must be installed in your application to use the previous code example.

[View sample in GitHub](https://github.com/SyncfusionExamples/open-save-pdf-documents-in-aws-s3/tree/master/Open%20and%20Save%20PDF%20in%20AWS%20S3%20using%20Standalone).

## Using Server-Backed PDF Viewer

To load a PDF file from AWS S3 in a PDF Viewer, you can follow the steps below

**Step 1:** Create a Simple PDF Viewer Sample in Vue
Expand Down Expand Up @@ -97,7 +186,7 @@ public async Task<IActionResult> Load([FromBody] Dictionary<string, string> json
}
```

6. Open the `appsettings.json` file in your web service project, Add the following lines below the existing `"AllowedHosts"` configuration
6. Open the `app settings.json` file in your web service project, Add the following lines below the existing `"AllowedHosts"` configuration

```json
{
Expand Down Expand Up @@ -186,4 +275,4 @@ export default {

N> The **AWSSDK.S3** NuGet package must be installed in your application to use the previous code example.

[View sample in GitHub](https://github.com/SyncfusionExamples/open-save-pdf-documents-in-aws-s3)
[View sample in GitHub](https://github.com/SyncfusionExamples/open-save-pdf-documents-in-aws-s3/tree/master/Open%20and%20Save%20PDF%20in%20AWS%20S3%20using%20Server-Backend)
141 changes: 138 additions & 3 deletions ej2-vue/pdfviewer/save-pdf-file/to-amazon-s3.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,142 @@ documentation: ug
domainurl: ##DomainURL##
---

# Save PDF file to AWS S3
# Save PDF file to AWS S3

PDF Viewer allows to save PDF file to AWS S3 using either the Standalone or Server-backed PDF Viewer. Below are the steps and a sample to demonstrate how to save PDF to AWS S3.

## Using Standalone PDF Viewer

To load a PDF file from AWS S3 in a PDF Viewer, you can follow the steps below.

**Step 1:** Create a PDF Viewer sample in Vue

Follow the instructions provided in this [link](https://ej2.syncfusion.com/vue/documentation/pdfviewer/getting-started) to create a simple PDF Viewer sample in Vue. This will set up the basic structure of your PDF Viewer application.

**Step 2:** Modify the `src/App.vue` File in the Vue Project

1. Import the required namespaces at the top of the file:

{% tabs %}
{% highlight html tabtitle="Options API (~/src/App.vue)" %}

<script>
import AWS from 'aws-sdk';
</script>

{% endhighlight %}
{% endtabs %}

2. Configures AWS SDK with the region, access key, and secret access key. This configuration allows the application to interact with AWS services like S3.

N> Replace **Your Region** with the actual Region of your AWS S3 account and **Your Access Key** with the actual Access Key of your AWS S3 account and **Your Security Access Key** with the actual Security Access Key of your AWS S3 account.

{% tabs %}
{% highlight html tabtitle="Options API (~/src/App.vue)" %}

<script>
AWS.config.update({
region: '**Your Region**', // Update this your region
accessKeyId: '*Your Access Key*', // Update this with your access key id
secretAccessKey: '*Your Security Access Key*', // Update this with your secret access key
});
</script>

{% endhighlight %}
{% endtabs %}

3. Configure a custom toolbar item for the download function to save a PDF file in Azure Blob Storage.

{% tabs %}
{% highlight html tabtitle="Options API (~/src/App.vue)" %}

<template>
<ejs-pdfviewer
id="pdfViewer"
:resourceUrl="resourceUrl"
:toolbarClick="toolbarClick"
:created="loadPdfDocument"
:toolbarSettings="toolbarSettings">
</ejs-pdfviewer>
</template>

<script>
export default {
data() {
let toolItem1 = {
prefixIcon: 'e-icons e-pv-download-document-icon',
id: 'download_pdf',
tooltipText: 'Download file',
align: 'right'
};

return {
resourceUrl: 'https://cdn.syncfusion.com/ej2/23.1.43/dist/ej2-pdfviewer-lib',
toolbarSettings: {
toolbarItems: [ 'OpenOption', 'PageNavigationTool', 'MagnificationTool', 'PanTool', 'SelectionTool', 'SearchOption', 'PrintOption', toolItem1, 'UndoRedoTool', 'AnnotationEditTool', 'FormDesignerEditTool', 'CommentTool', 'SubmitForm']
},
};
},

methods: {
toolbarClick: function (args) {
if (args.item && args.item.id === 'download_pdf') {
this.savePdfDocument();
}
},
}
}
</script>

{% endhighlight %}
{% endtabs %}

4. Retrieve the PDF viewer instance and save the current PDF as a Blob. Then, read the Blob using a FileReader to convert it into an ArrayBuffer, and upload the ArrayBuffer to AWS S3 using the putObject method of the S3 instance.

N> Replace **Your Bucket Name** with the actual Bucket name of your AWS S3 account and **Your Key** with the actual File Key of your AWS S3 account.

{% tabs %}
{% highlight html tabtitle="Options API (~/src/App.vue)" %}

<script>
export default {
methods: {
savePdfDocument: function () {
var viewer = document.getElementById('pdfViewer').ej2_instances[0];
viewer.saveAsBlob().then(function (value) {
var reader = new FileReader();
reader.onload = () => {
const uint8Array = new Uint8Array(reader.result);
const putObjectParams = {
Bucket: '**Your Bucket Name**',
Key: '**Your Key**',
Body: uint8Array,
ContentType: 'application/pdf',
};
var s3= new AWS.S3();
s3.putObject(putObjectParams, (err, data) => {
if (err) {
console.error('Error uploading document:', err);
} else {
console.log('Document uploaded successfully:', data);
}
});
};
reader.readAsArrayBuffer(value);
});
}
}
}
</script>

{% endhighlight %}
{% endtabs %}

N> The **npm install aws-sdk** package must be installed in your application to use the previous code example.

[View sample in GitHub](https://github.com/SyncfusionExamples/open-save-pdf-documents-in-aws-s3/tree/master/Open%20and%20Save%20PDF%20in%20AWS%20S3%20using%20Standalone).

## Using Server-Backed PDF Viewer

To save a PDF file to AWS S3, you can follow the steps below

Expand Down Expand Up @@ -87,7 +222,7 @@ public IActionResult Download([FromBody] Dictionary<string, string> jsonObject)
}
```

6. Open the `appsettings.json` file in your web service project, Add the following lines below the existing `"AllowedHosts"` configuration
6. Open the `app settings.json` file in your web service project, Add the following lines below the existing `"AllowedHosts"` configuration

```json
{
Expand Down Expand Up @@ -176,4 +311,4 @@ export default {

N> The **AWSSDK.S3** NuGet package must be installed in your application to use the previous code example.

[View sample in GitHub](https://github.com/SyncfusionExamples/open-save-pdf-documents-in-aws-s3)
[View sample in GitHub](https://github.com/SyncfusionExamples/open-save-pdf-documents-in-aws-s3/tree/master/Open%20and%20Save%20PDF%20in%20AWS%20S3%20using%20Server-Backend)