【发布时间】:2021-06-01 08:50:25
【问题描述】:
在下面的代码中,我正在生成收据并将其下载为本地的 pdf 格式。在此之前,我以模式显示它,即反应原生视图代码。现在对于 pdf 的东西,我正在使用一个库“react-native- html到pdf”。但它只转换 html 。在下面的第二部分中是我的反应原生格式的收据代码。请帮助我如何将其转换为 html 或如何做到这一点。 “saleRequest”是我在道具中获得的一个数组,所有数据都在这里可用,我需要生成收据。 请帮忙
const createPDF = async (props) => {
const {saleRequest}=props.navigation.state.params;
if (await isPermitted()) {
const message = "hello";
let options = {
//Content to print
html:
`<h1 style="text-align: center;"><strong>${message}</strong></h1><p style="text-align: center;">Here is an example of pdf Print in React Native</p><p style="text-align: center;"><strong>Team About React</strong></p>`,
//File Name
fileName: 'receipt',
//File directory
directory: 'documents',
};
let file = await RNHTMLtoPDF.convert(options);
setFilePath(file.filePath);
}
};
//// 下面的代码我必须下载到 pdf 中,但它是 react native jsx
<View style={{ width: deviceWidth - 30, }}>
<View style={{ marginTop: hp('2%'), flex: 1, justifyContent: 'flex-start', alignItems: 'center', flexDirection: 'row', width: wp('100%') }}>
<View style={{ flex: 1,left:wp('2%') }}>
<RegularText text={saleRequest.custName} textColor='#231F20' style={{ fontSize: hp('2%'), }} />
<RegularText text={saleRequest.mobileNum} textColor='#231F20' style={{ fontSize: hp('2%'), fontWeight: 'bold' }} />
</View>
{
!_.isEmpty(saleRequest.receiptNum) ?
<View style={{ flex: 1 }}>
<RegularText text='Receipt Number' textColor='#808080' style={{ fontSize: hp('2%'), }} />
<RegularText text={saleRequest.receiptNum} textColor='#231F20' style={{ fontSize: hp('2%'), fontWeight: 'bold' }} />
</View>
:
<View style={{ flex: 1 }}>
<RegularText text='Transaction Date' textColor='#808080' style={{ fontSize: hp('2%'), }} />
<RegularText text={saleRequest.transDate} textColor='#231F20' style={{ fontSize: hp('2%'), fontWeight: 'bold' }} />
</View>
}
</View>
<View style={{ marginTop: hp('2%'), flex: 1, justifyContent: 'flex-start', alignItems: 'center', flexDirection: 'row', width: wp('100%') }}>
<View style={{ flex: 1,left:wp('2%') }}>
<RegularText text='Txn#' textColor='#808080' style={{ fontSize: hp('2%'), }} />
<RegularText text={saleRequest.transNumber} textColor='#231F20' style={{ fontSize: hp('2%'), fontWeight: 'bold' }} />
</View>
<View style={{ flex: 1 }}>
<RegularText text='Payment Method' textColor='#808080' style={{ fontSize: hp('2%'), }} />
{
!_.isEmpty(saleRequest.payCash) &&
<RegularText numberOfLines={4} text={saleRequest.payCash} textColor='#231F20' style={{ paddingRight : 60,fontSize: hp('2%'), fontWeight: 'bold' }} />
}
{
!_.isEmpty(saleRequest.payCc) &&
<RegularText numberOfLines={4} text={saleRequest.payCc} textColor='#231F20' style={{ paddingRight : 60,fontSize: hp('2%'), fontWeight: 'bold' }} />
}
{
!_.isEmpty(saleRequest.payDc) &&
<RegularText numberOfLines={4} text={saleRequest.payDc} textColor='#231F20' style={{ paddingRight : 60,fontSize: hp('2%'), fontWeight: 'bold' }} />
}
{
_.isEmpty(saleRequest.payCash) &&
<RegularText text={'Instalment'} textColor='#231F20' style={{ fontSize: hp('2%'), fontWeight: 'bold' }} />
}
</View>
</View>
{!_.isEmpty(saleRequest.payCash) &&
<View style={{ flex: 1, flexDirection: 'row', marginRight : 100, marginTop: 10 }}>
<RegularText text='Cash Amount' textColor='#838384' style={{ flex:1,fontSize: hp('2%'),left:wp('2%') }} />
<RegularText text={saleRequest.payCashAmt} textColor='#231F20' style={{ fontSize: hp('2%'), fontWeight: 'bold' }} />
</View>
}
{
!_.isEmpty(saleRequest.payCc) &&
<View style={{ flex: 1, flexDirection: 'row', marginRight : 100,marginTop: 10 }}>
<RegularText text={`Credit Card Amount`} style={{ flex: 1, fontSize: hp('2%'), color: '#838384' }} />
<RegularText text={saleRequest.payCcAmt} textColor='#231F20' style={{ fontSize: hp('2%'), fontWeight: 'bold' }} />
</View>
}
{
!_.isEmpty(saleRequest.payDc) &&
<View style={{ flex: 1, flexDirection: 'row', marginRight : 100, marginTop: 10 }}>
<RegularText text={`Debit Card Amount`} style={{ flex: 1, fontSize: hp('2%'), color: '#838384' }} />
<RegularText text={saleRequest.payDcAmt} textColor='#231F20' style={{ fontSize: hp('2%'), fontWeight: 'bold' }} />
</View>
}
<View style={{
paddingBottom: 5, paddingTop: 20, borderBottomColor: '#231F20B3',
borderBottomWidth: 1, flex: 1, justifyContent: 'flex-start', alignItems: 'center', flexDirection: 'row', width: wp('100%'),
}}>
<RegularText text='Items' textColor='#231F20' style={{ flex: .4, fontSize: hp('1.5%'),left:wp('2%') }} />
<RegularText text='QTY' textColor='#231F20' style={{ flex: .2, fontSize: hp('1.5%'), textAlign: 'center' }} />
<RegularText text='Price' textColor='#231F20' style={{ flex: .2, fontSize: hp('1.5%'), }} />
<RegularText text='Amount' textColor='#231F20' style={{ flex: .2, fontSize: hp('1.5%'), }} />
</View>
{
saleRequest.itemData.map((data, index) => {
return (
<View key={index} style={{
paddingBottom: 10, paddingTop: 10, borderTopColor: '#D2D2D2', borderTopWidth: index !== 0 ? 1 : 0, flex: 1,
justifyContent: 'flex-start', alignItems: 'center', flexDirection: 'row', width: wp('100%')
}}>
<View style={{ flex: .4, justifyContent: 'center',left:wp('2%') }}>
<RegularText text={data.itemDescV} textColor='#808080' style={{ fontSize: hp('1.5%'), flex: .4 }} />
</View>
<RegularText text={data.quantityN} textColor='#231F20' style={{ flex: .2, fontSize: hp('1.5%'), textAlign: 'center', }} />
<RegularText text={data.itemPrice} textColor='#231F20' style={{ flex: .2, fontSize: hp('1.5%') }} />
<RegularText text={data.quantityN * data.itemPrice} textColor='#231F20' style={{ flex: .2, fontSize: hp('1.5%') }} />
</View>
)
})
}
<View style={{ borderBottomColor: '#231F20B3', borderBottomWidth: 1 }} />
<View style={{ flex: 1, width: wp('100%') - 60, paddingTop: 10 }}>
<View style={{ flexDirection: 'row' }}>
<RegularText text='Gross Total :' textColor='#231F20' style={{ flex: .6, fontSize: hp('1.5%'), textAlign: 'right' }} />
<RegularText text={saleRequest.grossTot} textColor='#231F20' style={{ flex: .4, fontSize: hp('1.5%'), textAlign: 'right', fontWeight: 'bold' }} />
</View>
<View style={{ flexDirection: 'row' }}>
<RegularText text='Discounts :' textColor='green' style={{ flex: .6, fontSize: hp('1.5%'), textAlign: 'right' }} />
<RegularText text={saleRequest.totItemDisc} textColor='green' style={{ flex: .4, fontSize: hp('1.5%'), textAlign: 'right', fontWeight: 'bold' }} />
</View>
{!isCredit&&<View style={{ flexDirection: 'row' }}>
<RegularText text='Transaction Discount :' textColor='green' style={{ flex: .6, fontSize: hp('1.5%'), textAlign: 'right' }} />
<RegularText text={saleRequest.totTransDisc} textColor='green' style={{ flex: .4, fontSize: hp('1.5%'), textAlign: 'right', fontWeight: 'bold' }} />
</View>}
<View style={{ flexDirection: 'row' }}>
<RegularText text='Total Amount :' textColor='#231F20' style={{ flex: .6, fontSize: hp('1.5%'), textAlign: 'right' }} />
<RegularText text={isCredit? "PGK " + parseInt(saleRequest.grossTot.slice(4) - saleRequest.totItemDisc.slice(4)) + ".00" : saleRequest.totalNet} textColor='#231F20' style={{ flex: .4, fontSize: hp('1.5%'), textAlign: 'right', fontWeight: 'bold' }} />
</View>
{!isCredit &&<View style={{ flexDirection: 'row' }}>
<RegularText text='VAT :' textColor='#231F20' style={{ flex: .6, fontSize: hp('1.5%'), textAlign: 'right' }} />
<RegularText text={saleRequest.vat} textColor='#231F20' style={{ flex: .4, fontSize: hp('1.5%'), textAlign: 'right', fontWeight: 'bold' }} />
</View>}
<View style={{ flexDirection: 'row' }}>
<RegularText text='Paid Amount :' textColor='#231F20' style={{ flex: .6, fontSize: hp('2%'), textAlign: 'right', fontWeight: 'bold' }} />
<RegularText text={isCredit ? "PGK 0" : saleRequest.totalPaid} textColor='#231F20' style={{ flex: .4, fontSize: hp('2%'), textAlign: 'right', fontWeight: 'bold' }} />
</View>
</View>
</View>
【问题讨论】:
标签: javascript html reactjs react-native react-native-navigation