Skip to content

Commit

Permalink
Merge pull request #1 from ArchireportTeam/fix/removePublicDir
Browse files Browse the repository at this point in the history
fix: remove on android public dir usage
  • Loading branch information
mlecoq committed Dec 23, 2021
2 parents 3fcb0cd + 3d469ce commit 779e42a
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 30 deletions.
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,13 @@ const pathOfGeneratedPdf = await generatePdf({

where

| param | description | example |
| ------------ | -------------------------------------------------- | ------------------------------------------------------------------------------------------------ |
| path | path to your html file | `${DocumentDirectoryPath}/index.html` (_DocumentDirectoryPath_ is provided by `react-native-fs`) |
| target | url used to indicate that js execution is finished | `http://finishload.com` |
| documentName | name of the generated pdf (in cache directory) | `file.pdf` |
| width | width of the generated pdf | 612 |
| height | height of the generated pdf | 792 |
| param | description | example |
| ------------ | --------------------------------------------------- | ------------------------------------------------------------------------------------------------ |
| path | path to your html file (it must be under documents) | `${DocumentDirectoryPath}/index.html` (_DocumentDirectoryPath_ is provided by `react-native-fs`) |
| target | url used to indicate that js execution is finished | `http://finishload.com` |
| documentName | name of the generated pdf (in cache directory) | `file.pdf` |
| width | width of the generated pdf | 612 |
| height | height of the generated pdf | 792 |

## Contributing

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,7 @@ open class StaticHtmlToPdfConverter(private var context: Context, private var pr
override fun run() {

val webView = WebView(context)
val publicDir = File(context.filesDir, "public")
val assetLoader = WebViewAssetLoader.Builder().addPathHandler("/public/", WebViewAssetLoader.InternalStoragePathHandler(context, publicDir)).build()
val assetLoader = WebViewAssetLoader.Builder().addPathHandler("/", WebViewAssetLoader.InternalStoragePathHandler(context, context.filesDir)).build()

webView.webViewClient = object : WebViewClient() {
@RequiresApi(21)
Expand Down
5 changes: 2 additions & 3 deletions example/src/Generated.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
import React from 'react';
import Share from 'react-native-share';
import { Box, Button, VStack, Input } from 'native-base';
import { Box, Button, VStack } from 'native-base';
import Pdf from 'react-native-pdf';
import { RouteProp, useRoute } from '@react-navigation/core';
import type { ParamsList } from './types';

export default function Generated() {
const route = useRoute<RouteProp<ParamsList, 'Generated'>>();

console.log('Generated File ', route.params.uri);
return (
<Box flex={1} padding="4">
<Input value={route.params.uri || ''} />
<VStack space={3} flex="1">
<Pdf source={{ uri: route.params.uri }} style={{ flex: 1 }} />
<Button
Expand Down
19 changes: 8 additions & 11 deletions example/src/Home.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
exists,
} from 'react-native-fs';
import { unzip } from 'react-native-zip-archive';
import { generatePdf } from 'react-native-static-html-to-pdf';
import { generatePdf } from '@archireport/react-native-static-html-to-pdf';
import { NavigationProp, useNavigation } from '@react-navigation/native';
import type { ParamsList } from './types';

Expand Down Expand Up @@ -48,15 +48,13 @@ export default function App() {

const contentType = res.info().headers['Content-Type'];

await mkdir(`${DocumentDirectoryPath}/public`);

if (contentType.includes('application/zip')) {
const targetPath = `file://${DocumentDirectoryPath}/public`;
const targetPath = `file://${DocumentDirectoryPath}`;

await unzip(path, targetPath);

const pdf = await generatePdf({
path: `${DocumentDirectoryPath}/public/export/default/index.html`,
path: `${DocumentDirectoryPath}/export/default/index.html`,
target: 'http://finishload.com',
documentName: 'file.pdf',
width: 612,
Expand All @@ -67,19 +65,19 @@ export default function App() {
} else {
const urlObj = new URL(url);

const directory = `${DocumentDirectoryPath}/public${urlObj.pathname}`;
const directory = `${DocumentDirectoryPath}${urlObj.pathname}`;
await mkdir(directory);

const targetPath = `file://${directory}/index.html`;

if (await exists(targetPath)) {
await unlink(targetPath);
}
console.log({ targetPath });

await moveFile(path, targetPath);

const pdf = await generatePdf({
path: `${DocumentDirectoryPath}/public${urlObj.pathname}/index.html`,
path: `${DocumentDirectoryPath}${urlObj.pathname}/index.html`,
target: 'http://finishload.com',
documentName: 'file.pdf',
width: 592,
Expand Down Expand Up @@ -113,10 +111,9 @@ export default function App() {
const path = res.path();

const contentType = res.info().headers['Content-Type'];
await mkdir(`${DocumentDirectoryPath}/public`);

if (contentType.includes('application/zip')) {
const targetPath = `file://${DocumentDirectoryPath}/public`;
const targetPath = `file://${DocumentDirectoryPath}`;

await unzip(path, targetPath);

Expand All @@ -126,7 +123,7 @@ export default function App() {
} else {
const urlObj = new URL(url);

const directory = `${DocumentDirectoryPath}/public${urlObj.pathname}`;
const directory = `${DocumentDirectoryPath}${urlObj.pathname}`;
await mkdir(directory);

const targetPath = `file://${directory}/index.html`;
Expand Down
9 changes: 3 additions & 6 deletions example/src/Preview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
useRoute,
} from '@react-navigation/core';
import React from 'react';
import { generatePdf } from 'react-native-static-html-to-pdf';
import { generatePdf } from '@archireport/react-native-static-html-to-pdf';
import { WebView } from 'react-native-webview';
import { Box, VStack, Button, Spinner } from 'native-base';
import type { ParamsList } from './types';
Expand All @@ -28,7 +28,7 @@ export default function Preview() {
React.useEffect(() => {
const server = new StaticServer(
8084,
`${DocumentDirectoryPath}/public/${route.params.uri}`
`${DocumentDirectoryPath}/${route.params.uri}`
);
serverRef.current = server;

Expand Down Expand Up @@ -63,9 +63,6 @@ export default function Preview() {
uri: url,
}}
onShouldStartLoadWithRequest={(request) => {
console.log('ploup', request.url);
console.log('plip', request.url.startsWith(url));

// Only allow navigating within this website
return request.url.startsWith(url);
}}
Expand All @@ -84,7 +81,7 @@ export default function Preview() {
try {
setLoading(true);
const pdf = await generatePdf({
path: `${DocumentDirectoryPath}/public${route.params.uri}/index.html`,
path: `${DocumentDirectoryPath}${route.params.uri}/index.html`,
target: 'http://finishload.com',
documentName: 'file.pdf',
width: 612,
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"compilerOptions": {
"baseUrl": "./",
"paths": {
"react-native-static-html-to-pdf": ["./src/index"]
"@archireport/react-native-static-html-to-pdf": ["./src/index"]
},
"allowUnreachableCode": false,
"allowUnusedLabels": false,
Expand Down

0 comments on commit 779e42a

Please sign in to comment.