Skip to content

Commit ff345f2

Browse files
authored
Merge pull request #538 from JaneaSystems/consume-rn-fetch-blob-windows
windows: import rn-fetch-blob on Windows
2 parents 5549125 + 15f8719 commit ff345f2

File tree

5 files changed

+44
-25
lines changed

5 files changed

+44
-25
lines changed

README.md

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,10 @@ So you should install react-native-pdf and rn-fetch-blob
2525
| progress-bar-android | | | | 1.0.3+ |
2626
| progress-view | | | | 1.0.3+ |
2727

28-
Currently, Windows support is partial. The rn-fetch-blob module is not yet available on Windows, only loading bundled PDFs is supported.
28+
Currently, Windows support is partial. For Windows, it's necessary to install `rn-fetch-blob` from the [PR that adds Windows support](https://github.com/joltup/rn-fetch-blob/pull/701):
29+
```
30+
yarn add github:joltup/rn-fetch-blob#pull/701/head
31+
```
2932

3033
### Installation
3134

@@ -37,6 +40,11 @@ npm install react-native-pdf rn-fetch-blob @react-native-community/progress-bar-
3740
yarn add react-native-pdf rn-fetch-blob @react-native-community/progress-bar-android @react-native-community/progress-view
3841
```
3942

43+
For Windows, it's necessary to install `rn-fetch-blob` from the [PR that adds Windows support](https://github.com/joltup/rn-fetch-blob/pull/701):
44+
```
45+
yarn add github:joltup/rn-fetch-blob#pull/701/head
46+
```
47+
4048
Then follow the instructions for your platform to link react-native-pdf into your project:
4149

4250
### iOS installation
@@ -96,12 +104,14 @@ react-native link react-native-pdf
96104
- Right-click Solution icon in Solution Explorer > Add > Existing Project...
97105
- Add `node_modules\@react-native-community\progress-view\windows\progress-view\progress-view.vcxproj`
98106
- If running RNW 0.62: add `node_modules\react-native-pdf\windows\RCTPdf\RCTPdf.vcxproj`
107+
- If running RNW 0.62: add `node_modules\rn-fetch-blob\windows\RNFetchBlob\RNFetchBlob.vcxproj`
99108
- Right-click main application project > Add > Reference...
100109
- Select `progress-view` and in Solution Projects
101-
- If running 0.62, also select `RCTPdf`
110+
- If running 0.62, also select `RCTPdf` and `RNFetchBlob`
102111
- In app `pch.h` add `#include "winrt/progress_view.h"` and `#include "winrt/RCTPdf.h"`
112+
- If running 0.62, also select `#include "winrt/RNFetchBlob.h"`
103113
- In `App.cpp` add `PackageProviders().Append(winrt::progress_view::ReactPackageProvider());` before `InitializeComponent();`
104-
- If running RNW 0.62, also add `PackageProviders().Append(winrt::RCTPdf::ReactPackageProvider());`
114+
- If running RNW 0.62, also add `PackageProviders().Append(winrt::RCTPdf::ReactPackageProvider());` and `PackageProviders().Append(winrt::RNFetchBlob::ReactPackageProvider());`
105115

106116
#### Bundling PDFs with the app
107117
To add a `test.pdf` like in the example add:
@@ -299,7 +309,7 @@ const styles = StyleSheet.create({
299309

300310
| Property | Type | Default | Description | iOS | Android | Windows | FirstRelease |
301311
| ------------- |:-------------:|:----------------:| ------------------- | ------| ------- | ------- | ------------ |
302-
| source | object | not null | PDF source like {uri:xxx, cache:false}. see the following for detail.||| partial | <3.0 |
312+
| source | object | not null | PDF source like {uri:xxx, cache:false}. see the following for detail.||| | <3.0 |
303313
| page | number | 1 | initial page index |||| <3.0 |
304314
| scale | number | 1.0 | should minScale<=scale<=maxScale|||| <3.0 |
305315
| minScale | number | 1.0 | min scale|||| 5.0.5 |
@@ -340,11 +350,11 @@ const styles = StyleSheet.create({
340350

341351
| Usage | Description | iOS | Android | Windows |
342352
| ------------ | ----------- | --- | ------- | ------- |
343-
| `{uri:"http://xxx/xxx.pdf"}` | load pdf from a url ||| |
353+
| `{uri:"http://xxx/xxx.pdf"}` | load pdf from a url ||| |
344354
| `{require("./test.pdf")}` | load pdf relate to js file (do not need add by xcode) ||||
345355
| `{uri:"bundle-assets://path/to/xxx.pdf"}` | load pdf from assets, the file should be at android/app/src/main/assets/path/to/xxx.pdf ||||
346356
| `{uri:"bundle-assets://xxx.pdf"}` | load pdf from assets, you must add pdf to project by xcode. this does not support folder. ||||
347-
| `{uri:"data:application/pdf;base64,JVBERi0xLjcKJc..."}` | load pdf from base64 string ||| |
357+
| `{uri:"data:application/pdf;base64,JVBERi0xLjcKJc..."}` | load pdf from base64 string ||| |
348358
| `{uri:"file:///absolute/path/to/xxx.pdf"}` | load pdf from local file system ||||
349359
| `{uri:"ms-appx:///xxx.pdf"}}` | load pdf bundled with UWP app ||||
350360

index.js

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,18 @@ import {
2121
import { ProgressBar } from '@react-native-community/progress-bar-android'
2222
import { ProgressView } from '@react-native-community/progress-view'
2323

24-
let RNFetchBlob = {
25-
fs : {
26-
dirs: {
27-
CacheDir: ''
28-
}
29-
}
30-
};
31-
if (Platform.OS !== 'windows') {
24+
let RNFetchBlob;
25+
try {
3226
RNFetchBlob = require('rn-fetch-blob').default;
27+
} catch(e) {
28+
// For Windows, when not using rn-fetch-blob with Windows support.
29+
RNFetchBlob = {
30+
fs : {
31+
dirs: {
32+
CacheDir: ''
33+
}
34+
}
35+
};
3336
}
3437

3538
const SHA1 = require('crypto-js/sha1');

windows/RCTPdf/RCTPdfControl.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,10 @@ namespace winrt::RCTPdf::implementation
6868
double currentRenderScale = renderScale;
6969
return currentRenderScale < imageScale || currentRenderScale > imageScale * m_downscaleTreshold;
7070
}
71-
winrt::IAsyncAction PDFPageInfo::render() {
71+
winrt::Windows::Foundation::IAsyncAction PDFPageInfo::render() {
7272
return render(imageScale);
7373
}
74-
winrt::IAsyncAction PDFPageInfo::render(double useScale) {
74+
winrt::Windows::Foundation::IAsyncAction PDFPageInfo::render(double useScale) {
7575
double currentRenderScale;
7676
while (true) {
7777
currentRenderScale = renderScale;
@@ -558,10 +558,14 @@ namespace winrt::RCTPdf::implementation
558558

559559
void RCTPdfControl::SetOrientation(bool horizontal) {
560560
m_horizontal = horizontal;
561-
FindName(winrt::to_hstring("OrientationSelector")).try_as<StackPanel>().Orientation(m_horizontal ? Orientation::Horizontal : Orientation::Vertical);
561+
StackPanel orientationSelector;
562+
if (FindName(winrt::to_hstring("OrientationSelector")).try_as<StackPanel>(orientationSelector))
563+
{
564+
orientationSelector.Orientation(m_horizontal ? Orientation::Horizontal : Orientation::Vertical);
565+
}
562566
}
563567

564-
winrt::IAsyncAction RCTPdfControl::RenderVisiblePages(int page) {
568+
winrt::Windows::Foundation::IAsyncAction RCTPdfControl::RenderVisiblePages(int page) {
565569
auto lifetime = get_strong();
566570
auto container = PagesContainer();
567571
auto currentHorizontalOffset = container.HorizontalOffset();

windows/RCTPdf/RCTPdfControl.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ namespace winrt::RCTPdf::implementation
1818
unsigned pageVisiblePixels(bool horizontal, double viewportStart, double viewportEnd) const;
1919
unsigned pageSize(bool horizontal) const;
2020
bool needsRender() const;
21-
winrt::IAsyncAction render();
22-
winrt::IAsyncAction render(double useScale);
21+
winrt::Windows::Foundation::IAsyncAction render();
22+
winrt::Windows::Foundation::IAsyncAction render(double useScale);
2323
unsigned height, width;
2424
unsigned scaledHeight, scaledWidth;
2525
unsigned scaledTopOffset, scaledLeftOffset;
@@ -96,7 +96,7 @@ namespace winrt::RCTPdf::implementation
9696
void GoToPage(int page);
9797
void Rescale(double newScale, double newMargin, bool goToNewPosition);
9898
void SetOrientation(bool horizontal);
99-
winrt::IAsyncAction RenderVisiblePages(int page);
99+
winrt::Windows::Foundation::IAsyncAction RenderVisiblePages(int page);
100100
void SignalError(const std::string& error);
101101
void SignalLoadComplete(int totalPages, int width, int height);
102102
void SignalPageChange(int page, int totalPages);

windows/README.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@ Since the module uses react-native-progress-view, it also needs to be referenced
55
- Right-click Solution icon in Solution Explorer > Add > Existing Project...
66
- Add `node_modules\@react-native-community\progress-view\windows\progress-view\progress-view.vcxproj`
77
- If running RNW 0.62: add `node_modules\react-native-pdf\windows\RCTPdf\RCTPdf.vcxproj`
8+
- If running RNW 0.62: add `node_modules\rn-fetch-blob\windows\RNFetchBlob\RNFetchBlob.vcxproj`
89
- Right-click main application project > Add > Reference...
910
- Select `progress-view` and in Solution Projects
10-
- If running 0.62, also select `RCTPdf`
11-
- In app `pch.h` add `#include "winrt/progress-view.h"` and `#include "winrt/RCTPdf.h"`
12-
- In `App.cpp` add `PackageProviders().Append(winrt::progress-view::ReactPackageProvider());` before `InitializeComponent();`
13-
- If running RNW 0.62, also add `PackageProviders().Append(winrt::RCTPdf::ReactPackageProvider());`
11+
- If running 0.62, also select `RCTPdf` and `RNFetchBlob`
12+
- In app `pch.h` add `#include "winrt/progress_view.h"` and `#include "winrt/RCTPdf.h"`
13+
- If running 0.62, also select `#include "winrt/RNFetchBlob.h"`
14+
- In `App.cpp` add `PackageProviders().Append(winrt::progress_view::ReactPackageProvider());` before `InitializeComponent();`
15+
- If running RNW 0.62, also add `PackageProviders().Append(winrt::RCTPdf::ReactPackageProvider());` and `PackageProviders().Append(winrt::RNFetchBlob::ReactPackageProvider());`
1416

1517

1618
## Bundling PDFs with the app

0 commit comments

Comments
 (0)