Skip to content
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

feat: handle foreignObject in svg to support html #7888

Merged
merged 6 commits into from
Nov 6, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 2 additions & 0 deletions packages/client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
"@sentry/tracing": "^7.12.1",
"@types/bcryptjs": "^2.4.2",
"@types/history": "^4.6.2",
"@types/html-to-pdfmake": "^2.4.4",
"@types/nock": "^10.0.3",
"@types/pdfmake": "^0.2.0",
"@types/react-redux": "^7.1.5",
Expand All @@ -66,6 +67,7 @@
"graphql-tools": "^4.0.7",
"handlebars": "^4.7.6",
"history": "^4.7.2",
"html-to-pdfmake": "^2.5.13",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

seems to be a fairly lightweight dependency, should be fine for bundle size👍

"iframe-resizer-react": "^1.1.0",
"jsdom-worker": "^0.3.0",
"jwt-decode": "^2.2.0",
Expand Down
38 changes: 36 additions & 2 deletions packages/client/src/views/PrintCertificate/PDFUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ import isValid from 'date-fns/isValid'
import format from 'date-fns/format'
import { getHandlebarHelpers } from '@client/forms/handlebarHelpers'
import { FontFamilyTypes } from '@client/utils/referenceApi'
import htmlToPdfmake from 'html-to-pdfmake'
import { Content } from 'pdfmake/interfaces'

type TemplateDataType = string | MessageDescriptor | Array<string>
function isMessageDescriptor(
Expand Down Expand Up @@ -224,8 +226,19 @@ src: url("${url}") format("truetype");
return serializer.serializeToString(svg)
}
export function svgToPdfTemplate(svg: string, offlineResource: IOfflineData) {
const initialDefaultFont = offlineResource.templates.fonts
? Object.keys(offlineResource.templates.fonts)[0]
: null
const pdfTemplate: IPDFTemplate = {
...certificateBaseTemplate,
definition: {
...certificateBaseTemplate.definition,
defaultStyle: {
font:
initialDefaultFont ||
certificateBaseTemplate.definition.defaultStyle.font
}
},
fonts: {
...certificateBaseTemplate.fonts,
...offlineResource.templates.fonts
Expand Down Expand Up @@ -253,9 +266,30 @@ export function svgToPdfTemplate(svg: string, offlineResource: IOfflineData) {
}
}

pdfTemplate.definition.content = {
svg
const foreignObjects = svgElement.getElementsByTagName('foreignObject')
const absolutelyPositionedHTMLs: Content[] = []
if (foreignObjects.length) {
tahmidrahman-dsi marked this conversation as resolved.
Show resolved Hide resolved
for (const foreignObject of foreignObjects) {
const x = Number.parseInt(foreignObject.getAttribute('x')!)
const y = Number.parseInt(foreignObject.getAttribute('y')!)
const htmlContent = foreignObject.innerHTML
const pdfmakeContent = htmlToPdfmake(htmlContent, {
ignoreStyles: ['font-family']
})
absolutelyPositionedHTMLs.push({
stack: pdfmakeContent,
absolutePosition: { x, y }
} as Content)
}
}

pdfTemplate.definition.content = [
{
svg
},
...absolutelyPositionedHTMLs
]

return pdfTemplate
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
*
* Copyright (C) The OpenCRVS Authors located at https://github.com/opencrvs/opencrvs-core/blob/master/AUTHORS.
*/
import { WORKQUEUE_TABS } from '@client/components/interface/Navigation'

Check warning on line 11 in packages/client/src/views/PrintCertificate/usePrintableCertificate.ts

View workflow job for this annotation

GitHub Actions / test (packages/client)

'WORKQUEUE_TABS' is defined but never used
import {
IPrintableDeclaration,
SUBMISSION_STATUS,

Check warning on line 14 in packages/client/src/views/PrintCertificate/usePrintableCertificate.ts

View workflow job for this annotation

GitHub Actions / test (packages/client)

'SUBMISSION_STATUS' is defined but never used
modifyDeclaration,
writeDeclaration
} from '@client/declarations'
Expand All @@ -19,9 +19,9 @@
import {
CorrectionSection,
IFormSectionData,
SubmissionAction

Check warning on line 22 in packages/client/src/views/PrintCertificate/usePrintableCertificate.ts

View workflow job for this annotation

GitHub Actions / test (packages/client)

'SubmissionAction' is defined but never used
} from '@client/forms'
import { goToCertificateCorrection, goToHomeTab } from '@client/navigation'

Check warning on line 24 in packages/client/src/views/PrintCertificate/usePrintableCertificate.ts

View workflow job for this annotation

GitHub Actions / test (packages/client)

'goToHomeTab' is defined but never used
import { getOfflineData } from '@client/offline/selectors'
import { getScope, getUserDetails } from '@client/profile/profileSelectors'
import { IStoreState } from '@client/store'
Expand Down Expand Up @@ -146,28 +146,28 @@
}
const draft = cloneDeep(declaration)

draft.submissionStatus = SUBMISSION_STATUS.READY_TO_CERTIFY
draft.action = isPrintInAdvance
? SubmissionAction.CERTIFY_DECLARATION
: SubmissionAction.CERTIFY_AND_ISSUE_DECLARATION
// draft.submissionStatus = SUBMISSION_STATUS.READY_TO_CERTIFY
// draft.action = isPrintInAdvance
// ? SubmissionAction.CERTIFY_DECLARATION
// : SubmissionAction.CERTIFY_AND_ISSUE_DECLARATION

const registeredDate = getRegisteredDate(draft.data)

Check warning on line 154 in packages/client/src/views/PrintCertificate/usePrintableCertificate.ts

View workflow job for this annotation

GitHub Actions / test (packages/client)

'registeredDate' is assigned a value but never used
const certificate = draft.data.registration.certificates[0]
const eventDate = getEventDate(draft.data, draft.event)

Check warning on line 156 in packages/client/src/views/PrintCertificate/usePrintableCertificate.ts

View workflow job for this annotation

GitHub Actions / test (packages/client)

'eventDate' is assigned a value but never used
if (!isPrintInAdvance) {
const paymentAmount = calculatePrice(
draft.event,
eventDate,
registeredDate,
offlineData
)
certificate.payments = {
type: 'MANUAL' as const,
amount: Number(paymentAmount),
outcome: 'COMPLETED' as const,
date: new Date().toISOString()
}
}
// if (!isPrintInAdvance) {
// const paymentAmount = calculatePrice(
// draft.event,
// eventDate,
// registeredDate,
// offlineData
// )
// certificate.payments = {
// type: 'MANUAL' as const,
// amount: Number(paymentAmount),
// outcome: 'COMPLETED' as const,
// date: new Date().toISOString()
// }
// }

const svg = await compileSvg(
offlineData.templates.certificates[draft.event].definition,
Expand All @@ -186,12 +186,12 @@
}

const pdfTemplate = svgToPdfTemplate(svg, offlineData)

console.log(pdfTemplate)

Check warning on line 189 in packages/client/src/views/PrintCertificate/usePrintableCertificate.ts

View workflow job for this annotation

GitHub Actions / test (packages/client)

Unexpected console statement
printPDF(pdfTemplate, draft.id)

dispatch(modifyDeclaration(draft))
dispatch(writeDeclaration(draft))
dispatch(goToHomeTab(WORKQUEUE_TABS.readyToPrint))
// dispatch(modifyDeclaration(draft))
// dispatch(writeDeclaration(draft))
// dispatch(goToHomeTab(WORKQUEUE_TABS.readyToPrint))
}

const handleEdit = () => {
Expand Down
Loading